컬λ μ μ΄ μ±μμ‘λμ§ λλ μ±μμ§κ³ μλμ§ μ μ μλλ‘ λ€μκ³Ό κ°μ νλͺ©μ΄ μμ μ μμ΅λλ€.
collection.fetch();
collection.isFetching()===true; // display loading
(collection.isFetching()===false; && collection.length) // display no records found
μ€μ μ¬μ© μ¬λ‘(λ¨μν, κ°μ²΄κ° μμ€ν κΉμμ΄ μ λ¬λ¨):
javascript()
// somewhere in the code
collection = new MyCollection();
// somewhere else in the code
view = new MyView({collection:collection});
// and somewhere else in the code
collection.fetch();
보기λ μμ§ μ νΈλ₯Ό μμ νκ³ λ‘λ/λ μ½λ μμ/λ μ½λ λͺ©λ‘μ νμν©λλ€.
λͺ¨λΈμ κ²½μ° μ μ₯/μμ /μ λ°μ΄νΈκ° μμ μλ μμ΅λλ€.
μλ
νμΈμ @g00fy-! μ΄λ₯Ό μν΄ "request"
λ° "sync"
μ΄λ²€νΈλ₯Ό μ¬μ©νλ κ²μ΄ μ’μ΅λλ€. μ΄λ»κ² μκ°νλμ?
collection.on('request', function() {
// loading...
}).on('sync', function() {
// display records or "none found"
});
collection.fetch();
_Edit - "fetch"
μλλΌ "request"
μ΄λ²€νΈλ₯Ό μλ―Ένμ΅λλ€._
λ©μ§ μμ΄λμ΄μΈ κ² κ°μμ @bradunbar!
@caseywebdev κ°μ¬ν©λλ€! :)
@g00fy-κ° μλνμ§ μμΌλ©΄ μλ €μ£Όμμμ€.
@braddunbar , @caseywebdev μμ΄λμ΄λ μ΄ λ λΆλΆμ λΆλ¦¬νλ κ²μ
λλ€. (λ·°λ _'clean'_ 컬λ μ
λΏλ§ μλλΌ λͺ¨λ μνμμ 컬λ μ
μ κ°μ Έμ¬ μ μμ΄μΌ ν©λλ€.)
μ νΈλ₯Ό μ¬μ©νμ¬ μ΄ μμ
μ μνν μ μλ€λ κ²μ μκ³ μμ§λ§ λ§μ κ²½μ° μ΄ λ°©λ²μΌλ‘ μνν μ μμ΅λλ€. νλμ μμκ° μ€μνλ€
λ·°κ° 'μμ²' λ° 'λκΈ°ν' μ νΈλ₯Ό μμ ν΄μΌ νλ€λ λ° λμνμ§λ§ 컬λ μ μ΄ λ·°μ μ λ¬λκΈ° μ μ μμ² μ νΈκ° μ μ‘λ μ μμΌλ―λ‘ λ·°λ 컬λ μ μ΄ κ°μ Έμ€λ κ²μ μμ§ λͺ»ν©λλ€.
λ³΄κΈ°κ° μμμ 컬λ μ μ κ°μ Έμ€λ μλ₯Ό κ³ λ €νμμμ€. μ΄ μ»¬λ μ μ λ·°μ μ λ¬λ λ _fetched_ , _fetching_ λλ _unfetched_ λ μ μμ΅λλ€ (tl:dr μμ 3 μ°Έμ‘°) .
collection = new Backbone.Collection();
view = new Backbone.View({collection:collection});
view.render();
// what should the view render ?
collection.fetch() // now sync signal gets sent
collection = new Backbone.Collection();
collection.fetch(); // now sync signal gets sent
// after fetch completes
// view won't be able to receive 'sync' signal
view = new Backbone.View({collection:collection});
view.render();
// at this point collection.length == 0.
// I guess the view can listen to 'sync' and then render 'empty'
collection = new Backbone.Collection();
collection.fetch(); // now 'request' signal gets sent, but it is pending
// view won't be able to receive 'request' signal
view = new Backbone.View({collection:collection}); // at this point collection.length == 0
// view did not receive the 'request' signal, therefore has no idea that the collection is fetching
// and the view should render 'loading'
// after 2 seconds, 'sync' signal gets sent
Viewλ collection.length == 0μ΄λ©΄ μΈμΆλμ§ μλλ€κ³ κ°μ ν΄μλ μ λ©λλ€.
Viewλ λν unfetch μν(λλ λ€λ₯Έ μν)μ 컬λ μ
μ΄ μ 곡λλ€κ³ κ°μ ν΄μλ μ λ©λλ€.
컬λ μ μ λ·°μμ λΆλ¦¬νλ €λ©΄ λ·°κ° μ»¬λ μ μνλ₯Ό μ μ μμ΄μΌ ν©λλ€.
λͺ¨λΈλ λ§μ°¬κ°μ§
@g00fy μ½λ μ
μ λ·°λ‘ λ³΄λΈ ν fetch
ν μ μλ€λ©΄ μ½λ μ
μ fetching
νλκ·Έ λ±μ μ€μ νμμμ€.
collection.on({
request: function () { this.fetching = true; },
'sync error': function () { this.fetching = false; }
});
μ΄μ 보기μμ this.collection.fetching
μ‘μΈμ€νκ³ this.collection
κ° sync
λλ error
μ΄λ²€νΈλ₯Ό μμνκΈ°λ₯Ό κΈ°λ€λ¦΄ μλ μμ΅λλ€.
@caseywebdev
μμ²μ΄ μ€λ¨λκ±°λ λ κ°μ μμ²μ΄ ν λ²μ μ μ‘λ μ μμ΅λλ€.
// example 1
collection.fetch().abort() // and collection.fetching == true
// example 2 - don't care about abort();
collection.fetch();
collection.fetch();
// 1st request compleated
!!collection.fetching == false // but the 2nd request is pending - should it be loading or not?
// 2nd request completed
μ΄μ λν ν΄κ²°μ± μ μ§μ°λ xhrμ μ°κ²°νλ κ²μ λλ€.
collection.on({
'request':function(model,xhr){
this.fetching = true;
xhr.fail(function(){this.fetching = false }.bind(this));
},
'sync error': function () { this.fetching = false; }
});
κ·Έλ κΈ° λλ¬Έμ λ΄μ₯λμ΄ μμΌλ©΄ νΈν κ² κ°μ§λ§, λ°λλ‘ μ΄λ° κ°μ μ¬νμ΄ λͺ¨λ ν΅κ³Όλλ©΄ BB μ½λ κΈ°λ°μ΄ λΆνλ €μ§ κ²μ λλ€.
μ΄μ κ°μ λͺ¨λ κ°μ μ¬νμ΄ μλ£λλ©΄ BB μ½λ κΈ°λ°μ΄ λΆνλ €μ§ κ²μ λλ€.
λλ κ·Έκ²μ΄ μ κ·Όμ κ΄ν κ²λ³΄λ€ ν½μ°½μ κ΄ν κ²μ΄λΌκ³ μκ°ν©λλ€. λμ μμ²μ μ²λ¦¬νλ λ°©λ²μλ μ¬λ¬ κ°μ§κ° μμ΅λλ€. λ°±λ³Έμ λΉμ μ΄ κ·Έκ²λ€μ μ²λ¦¬νλ λ° νμν ν리미ν°λΈλ₯Ό μ 곡νκ³ λ°©ν΄κ° λμ§ μλλ‘ ν©λλ€.
@g00fy- λͺ©μ μ μν΄ μ ν μν κΈ°κ³ λ₯Ό μμ±νλ λ°©λ²μ μ΄ν΄λ³΄κ³ μΆμ μλ μμ΅λλ€. Chaplin.jsμλ 볡μ νκΈ° μ¬μ΄ νλͺ©μ΄ μμΌλ©° νΉμ μ¬μ© μ¬λ‘μ μ ν©ν ifandelse/machina.js ( λΈλ‘κ·Έ κ²μλ¬Ό μ°Έμ‘°)λ μμ΅λλ€.
κ°μ₯ μ μ©ν λκΈ
@braddunbar , @caseywebdev μμ΄λμ΄λ μ΄ λ λΆλΆμ λΆλ¦¬νλ κ²μ λλ€. (λ·°λ _'clean'_ 컬λ μ λΏλ§ μλλΌ λͺ¨λ μνμμ 컬λ μ μ κ°μ Έμ¬ μ μμ΄μΌ ν©λλ€.)
μ νΈλ₯Ό μ¬μ©νμ¬ μ΄ μμ μ μνν μ μλ€λ κ²μ μκ³ μμ§λ§ λ§μ κ²½μ° μ΄ λ°©λ²μΌλ‘ μνν μ μμ΅λλ€. νλμ μμκ° μ€μνλ€
λ·°κ° 'μμ²' λ° 'λκΈ°ν' μ νΈλ₯Ό μμ ν΄μΌ νλ€λ λ° λμνμ§λ§ 컬λ μ μ΄ λ·°μ μ λ¬λκΈ° μ μ μμ² μ νΈκ° μ μ‘λ μ μμΌλ―λ‘ λ·°λ 컬λ μ μ΄ κ°μ Έμ€λ κ²μ μμ§ λͺ»ν©λλ€.
λ³΄κΈ°κ° μμμ 컬λ μ μ κ°μ Έμ€λ μλ₯Ό κ³ λ €νμμμ€. μ΄ μ»¬λ μ μ λ·°μ μ λ¬λ λ _fetched_ , _fetching_ λλ _unfetched_ λ μ μμ΅λλ€ (tl:dr μμ 3 μ°Έμ‘°) .
μ 1(μ νΈκ° μλνλ κ³³)
μ 2(보기λ μ΄λ―Έ κ°μ Έμμ§λ§ emty μμ§, μ νΈλ μλν¨)
μ 3(κ°μ₯ μ€μν κ²μ μ νΈκ° μλνμ§ μμ)
Viewλ collection.length == 0μ΄λ©΄ μΈμΆλμ§ μλλ€κ³ κ°μ ν΄μλ μ λ©λλ€.
Viewλ λν unfetch μν(λλ λ€λ₯Έ μν)μ 컬λ μ μ΄ μ 곡λλ€κ³ κ°μ ν΄μλ μ λ©λλ€.
컬λ μ μ λ·°μμ λΆλ¦¬νλ €λ©΄ λ·°κ° μ»¬λ μ μνλ₯Ό μ μ μμ΄μΌ ν©λλ€.
λͺ¨λΈλ λ§μ°¬κ°μ§