Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干-为什么';t collection.reset是否触发模型事件?_Javascript_Model View Controller_Backbone.js_Backbone Events - Fatal编程技术网

Javascript 主干-为什么';t collection.reset是否触发模型事件?

Javascript 主干-为什么';t collection.reset是否触发模型事件?,javascript,model-view-controller,backbone.js,backbone-events,Javascript,Model View Controller,Backbone.js,Backbone Events,我很想知道为什么重置主干集合不会触发模型事件。但是,当模型从集合中物理删除时触发模型事件似乎是合乎逻辑的 这是故意的还是我遗漏了什么?如果主干网不做这类事情,那么授权这样的事件的好做法是什么 为什么主干在其集合重置时不触发模型事件 var TicketModel = Backbone.Model.extend({ defaults: { name: 'crafty', email: 'dwq@dwqcqw.com' }, initializ

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

这是故意的还是我遗漏了什么?如果主干网不做这类事情,那么授权这样的事件的好做法是什么

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

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

这是主干重置功能:

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;
},
这里发生的事情是,我们将从模型对象中完全删除collection属性,并删除与此模型的事件的绑定。接下来我们调用集合的
\u reset()
-函数,如下所示:

_removeReference: function(model) {
  if (this == model.collection) {
    delete model.collection;
  }
  model.off('all', this._onModelEvent, this);
},
_reset: function(options) {
  this.length = 0;
  this.models = [];
  this._byId  = {};
  this._byCid = {};
}, 
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;
  }
});
它只是直接删除了对该系列曾经拥有的任何模型的任何引用

我们能从中得到什么?主干网中的收集重置-功能基本上只是绕过了所有移除模型的官方渠道,并在保密的情况下完成所有操作,不会引发除重置之外的任何其他事件。因此,您想在重置期间为从集合中移除的每个模型触发模型的
remove
事件吗?容易的!只需覆盖主干。集合的重置功能如下:

_removeReference: function(model) {
  if (this == model.collection) {
    delete model.collection;
  }
  model.off('all', this._onModelEvent, this);
},
_reset: function(options) {
  this.length = 0;
  this.models = [];
  this._byId  = {};
  this._byCid = {};
}, 
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;
  }
});
var Collection=Backbone.Collection.extend({
重置:功能(型号、选项){
模型| |(模型=[]);
选项| |(选项={});
对于(var i=0,l=this.models.length;i

希望这有帮助

在更新到另一个版本时,重写主干方法可能会导致痛苦

主干在options.previousModels中重置之前存储一组模型,所以只需侦听重置事件并在这些以前的模型上触发“删除”事件:

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

那就行了。

回答得很透彻。读起来也很愉快!谢谢!没问题,通过源代码找到这样一个问题的解决方案总是一个很好的学习体验!我想知道这背后的原因是什么。一个模型“移除”事件在集合中出现,为什么不反过来呢?我在这里发布了一个问题,谁知道我们可能会得到一些背景信息,解释为什么模型对集合的行为一无所知。很公平,如果这让事情更清楚的话,谢谢rimian