Backbone: μˆ˜μ§‘ 및 λͺ¨λΈ κ°€μ Έμ˜€κΈ°/μ €μž₯ λ“± μƒνƒœ

에 λ§Œλ“  2013λ…„ 01μ›” 24일  Β·  9μ½”λ©˜νŠΈ  Β·  좜처: jashkenas/backbone

μ»¬λ ‰μ…˜μ΄ μ±„μ›Œμ‘ŒλŠ”μ§€ λ˜λŠ” μ±„μ›Œμ§€κ³  μžˆλŠ”μ§€ μ•Œ 수 μžˆλ„λ‘ λ‹€μŒκ³Ό 같은 ν•­λͺ©μ΄ μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.


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();

λ³΄κΈ°λŠ” μˆ˜μ§‘ μ‹ ν˜Έλ₯Ό μˆ˜μ‹ ν•˜κ³  λ‘œλ“œ/λ ˆμ½”λ“œ μ—†μŒ/λ ˆμ½”λ“œ λͺ©λ‘μ„ ν‘œμ‹œν•©λ‹ˆλ‹€.

λͺ¨λΈμ˜ 경우 μ €μž₯/μ‚­μ œ/μ—…λ°μ΄νŠΈκ°€ μžˆμ„ μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

question

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

@braddunbar , @caseywebdev μ•„μ΄λ””μ–΄λŠ” 이 두 뢀뢄을 λΆ„λ¦¬ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. (λ·°λŠ” _'clean'_ μ»¬λ ‰μ…˜λΏλ§Œ μ•„λ‹ˆλΌ λͺ¨λ“  μƒνƒœμ—μ„œ μ»¬λ ‰μ…˜μ„ κ°€μ Έμ˜¬ 수 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.)
μ‹ ν˜Έλ₯Ό μ‚¬μš©ν•˜μ—¬ 이 μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆλ‹€λŠ” 것을 μ•Œκ³  μžˆμ§€λ§Œ λ§Žμ€ 경우 이 λ°©λ²•μœΌλ‘œ μˆ˜ν–‰ν•  수 μ—†μŠ΅λ‹ˆλ‹€. ν–‰λ™μ˜ μˆœμ„œκ°€ μ€‘μš”ν•˜λ‹€

λ·°κ°€ 'μš”μ²­' 및 '동기화' μ‹ ν˜Έλ₯Ό μˆ˜μ‹ ν•΄μ•Ό ν•œλ‹€λŠ” 데 λ™μ˜ν•˜μ§€λ§Œ μ»¬λ ‰μ…˜μ΄ 뷰에 μ „λ‹¬λ˜κΈ° 전에 μš”μ²­ μ‹ ν˜Έκ°€ 전솑될 수 μžˆμœΌλ―€λ‘œ λ·°λŠ” μ»¬λ ‰μ…˜μ΄ κ°€μ Έμ˜€λŠ” 것을 μ•Œμ§€ λͺ»ν•©λ‹ˆλ‹€.

보기가 μž„μ˜μ˜ μ»¬λ ‰μ…˜μ„ κ°€μ Έμ˜€λŠ” 예λ₯Ό κ³ λ €ν•˜μ‹­μ‹œμ˜€. 이 μ»¬λ ‰μ…˜μ€ 뷰에 전달될 λ•Œ _fetched_ , _fetching_ λ˜λŠ” _unfetched_ 될 수 μžˆμŠ΅λ‹ˆλ‹€ (tl:dr 예제 3 μ°Έμ‘°) .

예 1(μ‹ ν˜Έκ°€ μž‘λ™ν•˜λŠ” κ³³)

collection = new Backbone.Collection();
view = new Backbone.View({collection:collection});
view.render();
// what should the view render ?
collection.fetch() // now sync signal gets sent

예 2(λ³΄κΈ°λŠ” 이미 κ°€μ Έμ™”μ§€λ§Œ emty μˆ˜μ§‘, μ‹ ν˜Έλ„ μž‘λ™ν•¨)

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' 

예 3(κ°€μž₯ μ€‘μš”ν•œ 것은 μ‹ ν˜Έκ°€ μž‘λ™ν•˜μ§€ μ•ŠμŒ)

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 μƒνƒœ(λ˜λŠ” λ‹€λ₯Έ μƒνƒœ)의 μ»¬λ ‰μ…˜μ΄ μ œκ³΅λœλ‹€κ³  κ°€μ •ν•΄μ„œλŠ” μ•ˆ λ©λ‹ˆλ‹€.

μ»¬λ ‰μ…˜μ„ λ·°μ—μ„œ λΆ„λ¦¬ν•˜λ €λ©΄ λ·°κ°€ μ»¬λ ‰μ…˜ μƒνƒœλ₯Ό μ•Œ 수 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.

λͺ¨λΈλ„ λ§ˆμ°¬κ°€μ§€

λͺ¨λ“  9 λŒ“κΈ€

μ•ˆλ…•ν•˜μ„Έμš” @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 μ°Έμ‘°) .

예 1(μ‹ ν˜Έκ°€ μž‘λ™ν•˜λŠ” κ³³)

collection = new Backbone.Collection();
view = new Backbone.View({collection:collection});
view.render();
// what should the view render ?
collection.fetch() // now sync signal gets sent

예 2(λ³΄κΈ°λŠ” 이미 κ°€μ Έμ™”μ§€λ§Œ emty μˆ˜μ§‘, μ‹ ν˜Έλ„ μž‘λ™ν•¨)

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' 

예 3(κ°€μž₯ μ€‘μš”ν•œ 것은 μ‹ ν˜Έκ°€ μž‘λ™ν•˜μ§€ μ•ŠμŒ)

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 ( λΈ”λ‘œκ·Έ κ²Œμ‹œλ¬Ό μ°Έμ‘°)도 μžˆμŠ΅λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰