8

我很想知道为什么重置骨干集合不会触发模型事件。但是,当模型从集合中被物理删除时,触发模型事件似乎是合乎逻辑的。

这是故意的还是我错过了什么?如果骨干不做这种事情,那么委派这样的事件是一个好习惯。

为什么当其集合重置时主干不会触发模型事件?

var TicketModel = Backbone.Model.extend({
    defaults: {
        name: 'crafty',
        email: 'dwq@dwqcqw.com'
    },
    initialize: function(){
        this.on("all", function(event){
            console.log(event)
        });
    }

});

var TicketCollection = Backbone.Collection.extend({
    model: TicketModel,

    });


var tickets = new TicketCollection([
    {
        name: 'halldwq'
    },
    {
        name: 'dascwq'
    },
    {
        name: 'dsacwqe'
    }

]);

tickets.reset();
4

2 回答 2

16

这是主干重置功能:

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 行,因为您没有为 reset-function 提供任何模型。也让我们忽略前两行。所以首先我们遍历这个集合中的模型并调用集合的_removeReference(model)方法,它看起来像这样:

_removeReference: function(model) {
  if (this == model.collection) {
    delete model.collection;
  }
  model.off('all', this._onModelEvent, this);
},

这里发生的情况是,我们从模型对象中完全删除了集合属性,并且还删除了与该模型事件的绑定。接下来我们调用集合的_reset()-function,如下所示:

_reset: function(options) {
  this.length = 0;
  this.models = [];
  this._byId  = {};
  this._byCid = {};
}, 

它只是彻底删除了对该系列曾经拥有的任何模型的任何引用。

我们能从中得到什么?好吧,Backbone 中的 collectionreset功能基本上只是绕过了所有删除模型的官方渠道,并且完全保密,除了reset被解雇之外没有其他事件。所以你想remove在重置期间为从集合中删除的每个模型触发模型事件?简单的!只需像这样覆盖 Backbone.Collection 的重置功能:

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

希望这可以帮助!

于 2012-08-02T09:59:35.610 回答
16

更新到另一个版本时,覆盖 Backbone 方法可能会导致痛苦。

Backbone 将重置前的模型数组存储在 options.previousModels 中,因此只需监听重置事件并在这些先前的模型上触发“删除”事件:

collection.on('reset', function(col, opts){
   _.each(opts.previousModels, function(model){
        model.trigger('remove');
    });
});

这样就行了。

于 2013-02-08T17:19:28.137 回答