【发布时间】:2012-07-25 21:08:49
【问题描述】:
我正在尝试对 Marionette.CompositeView 中的集合进行排序。
我有一个看起来像这样的集合:
[
{id: 1, name: 'bar'},
{id: 2, name: 'boo' },
{id: 3, name: 'foo' }
]
我需要按 id 以相反的顺序对集合进行排序。
实际上它只有在我重新加载页面时才有效。
当我添加一个新模型时,新项目显然是随机添加到列表中的。
如果我刷新页面,它们将得到很好的排序。
我的问题是:
1) 添加新模型时如何解决问题?
2)有可能改进代码吗?
这是我的代码:
return Marionette.CompositeView.extend({
initialize: function () {
this.collection.fetch();
},
onRender: function () {
var collection = this.collection;
collection.comparator = function (collection) {
return - collection.get('id');
}
},
onSuccess: function () {
this.collection.add(this.messageModel);
this.collection.sort(); // the messageModel seems to be added
// apparently randomly to the list.
// only if I refresh the page it will be ok
}
})
【问题讨论】:
标签: javascript backbone.js marionette