これはバックボーンリセット機能です。
reset: function(models, options) {
models || (models = []);
options || (options = {});
for (var i = 0, l = this.models.length; i < l; i++) {
this._removeReference(this.models[i]);
}
this._reset();
this.add(models, _.extend({silent: true}, options));
if (!options.silent) this.trigger('reset', this, options);
return this;
},
リセット機能にモデルを指定しないため、最後の3行は無視できます。また、最初の2行も無視しましょう。したがって、最初にこのコレクションのモデルをループして、コレクションの_removeReference(model)
メソッドを呼び出します。次のようになります。
_removeReference: function(model) {
if (this == model.collection) {
delete model.collection;
}
model.off('all', this._onModelEvent, this);
},
ここで行われるのは、モデルオブジェクトからコレクションプロパティを完全に削除し、このモデルのイベントへのバインドも削除することです。_reset()
次に、コレクションの関数を呼び出します。これは次のようになります。
_reset: function(options) {
this.length = 0;
this.models = [];
this._byId = {};
this._byCid = {};
},
コレクションがこれまでに持っていたモデルへの参照を完全に削除します。
これから何ができるでしょうか?さて、Backboneのコレクション機能は、基本的に、モデルを削除するすべての公式チャネルを回避し、すべてをハッシュハッシュの秘密で実行し、起動されるreset
以外のイベントを引き起こしません。では、リセット中にコレクションから削除されたすべてのモデルに対してreset
モデルのイベントを発生させたいですか?remove
簡単!Backbone.Collectionのreset-functionを次のように上書きするだけです。
var Collection = Backbone.Collection.extend({
reset: function(models, options) {
models || (models = []);
options || (options = {});
for (var i = 0, l = this.models.length; i < l; i++) {
this._removeReference(this.models[i]);
// trigger the remove event for the model manually
this.models[i].trigger('remove', this.models[i], this);
}
this._reset();
this.add(models, _.extend({silent: true}, options));
if (!options.silent) this.trigger('reset', this, options);
return this;
}
});
お役に立てれば!