Collection
κ° λ΄λΆ νΌλ μνμ λΉ μ§κ² νλ λ°±λ³Έ μ ν리μΌμ΄μ
μ μΌλ ¨μ λ¨κ³μ λν΄ λ΄κ° μ§μν μ μλ κ°μ₯ 짧μ μ¬μμ°μ λν΄ μλλ₯Ό μ°Έμ‘°νμμμ€.
μμ μ¬λ³Έμ μ΄ JSFiddle μμ μ°Ύμ μ μμ΅λλ€.
무μμ 보μ¬μ£Όλ κ²μ μΈ collection
λ΄λΆ (μ μ§ _byId
(A)μ) κΈ°μ€ remove()
-ed Model
. Model
λ λ μ΄μ collection.models
μΌλΆκ° μλμ§λ§ (μ΄μ ) id
ν΅ν΄ κ³μ κ²μν μ μμ΅λλ€.
var
MyView = Backbone.View.extend( {
initialize: function () {
this.model = new Backbone.Model( {
id: 'foo'
, foo: 'bar'
} );
this.collection = new Backbone.Collection( this.model );
this.listenTo( this.model, 'change:id', function ( model, id ) {
if ( id == null ) {
this.collection.remove( model );
}
} );
this.model.unset( 'id' );
$('#models' ).val( JSON.stringify( this.collection.models ));
$('#_byId' ).val( JSON.stringify( this.collection._byId ));
$('#get-foo').val( JSON.stringify( this.collection.get( 'foo' )));
}
})
, foo = new MyView()
;
ν , ν₯λ―Έλ‘κ΅°. 컬λ μ μ λͺ¨λΈμ μΆκ°ν λ __byId_λ λͺ¨λΈμ λν μ°Έμ‘°μΈ model.idμ model.cidμ λ κ°μ§ μμ±μ μΆκ°ν©λλ€. 1134 νμ λν λ¬Έμ , id === undefined μ΄μ§λ§ __byId_μ model.id - _'foo'_κ° ν¬ν¨λμ΄ μμ΅λλ€.
model.idλ₯Ό value == null λ‘ λ³κ²½ν λ 컬λ μ μλ νΉλ³ν λ Όλ¦¬κ° μμ΅λλ€. 1183νμ νμΈνμΈμ.
λν λΉμ μ μΈνΈ _null_ λλ _undefined_μ ν΄μ λ³κ²½ν μ μμ΅λλ€. this.model.set('μμ΄λ', null);
μΆμ : λͺ¨λΈ IDλ₯Ό null λλ undefinedλ‘ μ€μ ν νμκ° μλ€κ³ μκ°ν©λλ€. μ΄λ μν° ν¨ν΄μ λλ€.
μ β μ λ μ΄λ κ² ν΄μλ μ λμ§λ§ μ¬μ ν λ²κ·Έμ λλ€.
id
κ° κ°μμ null
λλ©΄ λ¨Όμ delete this._byId[id]
μμ νμ¬ μ΄ λ¬Έμ λ₯Ό ν΄κ²°ν΄μΌ ν©λλ€.
μ΄λ²€νΈλ₯Ό μ¬μ©νμ§ μκ³ μ»¬λ μ
μμ λͺ¨λΈμ μ κ±°νλ©΄ μμλλ‘ μλν©λλ€. λ¬Έμ λ μ΄λ²€νΈ μ΄λ¦μ΄ change
μΈμ§ νμΈνλ _onModelEvent
μ μμ΅λλ€. μ΄ κ²½μ° μ μ‘λλ μ€μ μ΄λ²€νΈ μ΄λ¦μ change:id
μ
λλ€.
κ°μ₯ μ μ©ν λκΈ
μ β μ λ μ΄λ κ² ν΄μλ μ λμ§λ§ μ¬μ ν λ²κ·Έμ λλ€.
id
κ° κ°μμnull
λλ©΄ λ¨Όμ delete this._byId[id]
μμ νμ¬ μ΄ λ¬Έμ λ₯Ό ν΄κ²°ν΄μΌ ν©λλ€.