繁体   English   中英

为什么在相同条件下不会触发/完成Backbone Collection的“ sort”事件和排序动作本身?

[英]Why Backbone Collection 'sort' event and the sorting action itself are not triggered / done under the same conditions?

我有一个Backbone.Collection-> Backgrid相关的错误。 行为是在获取集合后,backgrid表将呈现两次。

我对此进行了调查,发现与Backbone.Collection上的“ sort”事件有关。

默认情况下,Backgrid正在侦听以下事件

this.listenTo(collection, "add", this.insertRow);
this.listenTo(collection, "remove", this.removeRow);
this.listenTo(collection, "sort", this.refresh);
this.listenTo(collection, "reset", this.refresh);
this.listenTo(collection, "backgrid:sort", this.sort);
this.listenTo(collection, "backgrid:edited", this.moveToNextCell);

我们没有对我们的收藏进行任何前端排序,所以我想知道为什么调用'sort'事件。

我在Backbone.Collection实现中对此进行了更多调查,发现以下内容:

提取后,将在集合上调用“ set”方法,其中包括以下代码块

// Silently sort the collection if appropriate.
  if (sort) this.sort({silent: true});

  // Unless silenced, it's time to fire all appropriate add/sort events.
  if (!options.silent) {
    var addOpts = at != null ? _.clone(options) : options;
    for (var i = 0; i < toAdd.length; i++) {
      if (at != null) addOpts.index = at + i;
      (model = toAdd[i]).trigger('add', model, this, addOpts);
    }
    if (sort || orderChanged) this.trigger('sort', this, options);
    if (toAdd.length || toRemove.length) this.trigger('update', this, options);
  }

在这里我们可以看到排序动作本身取决于'sort'变量。 就我而言,这是错误的,所以根本不进行任何排序。

但是,将基于“ sort”或“ orderChanged”为真来调用“ sort”事件本身。

棘手的是,即使对于导致此事件被触发的第一次提取,“ orderChanged”也为true。

作为解决方案 ,我只是阻止Backbone监听此事件(我们不进行任何UI排序,因此对于我们来说是可以接受的),但是我想知道为什么在相同条件下不执行/触发排序动作和sort事件。

我真的不知道backgrid以及您当前的实现是什么,但是我认为这可能来自于backgrid可能正在使用将比较器绑定到集合的事实。

然后,“在通常情况下,您无需调用此函数,因为只要添加模型,带有比较器的集合就会对其进行排序。”

http://backbonejs.org/#Collection-sort

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM