我正在嘗試對Marionette.CompositeView
中的集合進行排序。
我有一個集合,看起來像這樣:在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
}
})
GitHub的鏈接是死了:-( – ErichBSchulz 2013-04-25 10:31:14
GitHub的鏈接是不是死了:-) – Ziggy 2014-01-28 16:48:44
Github的鏈接又是死。 – abhaga 2014-09-02 12:38:21