クリック イベントでコレクションを再分類する際に問題が発生しています。モデルごとに並べ替え関数がトリガーされて実行されますが、リセット イベントがトリガーされたり、ビューでコレクションが変更されたりすることはありません。
私のコレクションには、次のような複数の並べ替え基準が定義されています。
feNoRequire.Collections.CompetitionCollection = Backbone.Collection.extend({
model: feNoRequire.Models.CompetitionModel,
comparator: function (property) {
return selectedStrategy.apply(model.get(property));
},
strategies: {
name: function (competition) { return competition.get("name"); },
nameReverse: function (competition) { console.log(competition); return -competition.get("name"); },
date: function (competition) { console.log(competition.get("event")); },
},
changeSort: function (sortProperty) {
this.comparator = this.strategies[sortProperty];
},
initialize: function () {
this.changeSort("name");
}
});
そして私のビューファイルで:
initialize: function(options){
this.evnt = options.evnt;
this.collection.on('reset', this.render, this);
this.evnt.bind("orderByDate", this.changeSort, this);
},
changeSort: function(){
this.collection.changeSort('nameReverse')
this.collection.sort();
},
render: function() {
console.log("going for rendering")
var renderedContent = this.template({competitions: this.collection.toJSON()});
$(this.el).html(renderedContent);
return this;
}
これを解決する方法について何か考えはありますか?
編集 以下の回答の後、レンダリングがトリガーされますが、オブジェクトは初期化時にのみソートされます。以降の並べ替えでは、最初の順序でコレクションが返されます - this.changeSort("name");
私のモデル:
feNoRequire.Models.CompetitionModel = Backbone.Model.extend({
initialize: function(){
this.attributes.events = new feNoRequire.Collections.EventCollection(this.attributes.events);
}
});