我在点击事件时遇到问题。排序功能被触发并为每个模型执行,但既没有触发重置事件,也没有在视图上更改集合。
我在我的收藏中定义了多个排序标准,例如:
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);
}
});