javascript - 主干,插入模型,同时保持排序顺序

标签 javascript backbone.js

添加新模型时(通过集合的“set”函数),我希望将模型插入到保持排序顺序的索引处,而不是插入到末尾。

谢谢

var Ts = (function () {
  var Result = Backbone.Model.extend({
   idAttribute : 'PAG_ID'
  });

  var ResultList = Backbone.Collection.extend({
    model: Result,
    comparator: function(result) {
     //console.log(result);
     return result.get('SORT_KEY');
   },
  });

var resultsCollection = new ResultList(data);

data = undefined;
var TableView = Backbone.View.extend({
 tagName: 'table',

initialize : function() {
    _.bindAll(this, 'render', 'renderRow');
    this.collection.on("add", this.renderRow, this);
},

render: function() {
  $(this.el).attr('id', 'tsTable').addClass('resulttable');
  this.renderHeader(this.collection.shift());
  this.collection.each(this.renderRow);
  return this;
},

renderHeader : function(model) {
  var col=new HeaderView({model:model});
  this.$el.append(col.render().$el);
  return this;
},

renderRow : function(model) {
  var row=new RowView({model:model});
  this.$el.append(row.render().$el);
  return this;
}

});

var HeaderView = Backbone.View.extend({
tagName: 'tr',

model: resultsCollection.models,

initialize: function() {
  this.model.on('change',this.render,this);
},

render: function() {
  var html=_.template(colTemplate,this.model.toJSON());
  this.$el.html(html);
  return this;
}

});

var RowView = Backbone.View.extend({
tagName: 'tr',

initialize: function() {
  this.model.on('all',this.render,this);
},

remove: function () {
    debug.log("Called remove event on model");
    $(this.el).remove();
},

model: resultsCollection.models,
  render: function() {
  var html=_.template(rowTemplate,this.model.toJSON());
  this.$el.html(html);
  return this;
},

attributes : function () {
  return {
    id : this.model.get('PAG_ID')
  };
}

});

var tableView = new TableView({collection: resultsCollection});
$("body").append( tableView.render().$el );


 resultsCollection.set(initialdata);
 resultsCollection.set(someotherdata, {merge: true});

我已经更改为如下并且它有效。不确定这是否是最好的实现

 renderRow : function(model) {
  var row = new RowView({model:model});
  var index = model.get('SORT_KEY') - 1;
  row.render().$el.insertAfter(this.$el.find('tr:eq('+ index  +')'));
  return this;
}

最佳答案

如果你在集合上提供比较器函数,Collection.set将在新模型拼接后执行静默排序。

来自 Backbone 来源http://backbonejs.org/docs/backbone.html :

set: function(models, options) {
  var sortable = this.comparator && (at == null) && options.sort !== false;
  var sortAttr = _.isString(this.comparator) ? this.comparator : null;
  ...

  if (toAdd.length) {
    if (sortable) sort = true;
    this.length += toAdd.length;
    if (at != null) {
      splice.apply(this.models, [at, 0].concat(toAdd));
    } else {
      push.apply(this.models, toAdd);
    }
  }

  if (sort) this.sort({silent: true});

这是一个 fiddle ,证明 collection.set 尊重比较器。

http://jsfiddle.net/puleos/sczV3/

关于javascript - 主干,插入模型,同时保持排序顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16699059/

相关文章:

javascript - 如何区分根本未指定::before 元素的内容属性和指定为空字符串?

javascript - 主干可重用 View (将新模型设置为现有 View )

javascript - 如果内容是 PAGE,SAP UI5 Popover 无法正确显示内容

javascript - 如何使用 Rails 数据库中的数据创建 d3 图形

javascript - 为计数器添加滑动效果

javascript - 有没有办法在插入html时触发事件?

javascript - ajaxPrefilter 与 Backbone

javascript - 主干模型的 fetch() 不返回所需的对象

javascript - 如何在一个 View 中获取多个集合

javascript - 调用异步函数返回带有属性名称数字的 Promise 对象