【发布时间】:2013-05-04 15:00:46
【问题描述】:
我在利用点击事件处理我的收藏时遇到问题。为每个模型触发并执行排序功能,但既不会触发重置事件,也不会触发视图上的集合。
我在我的收藏中定义了多个排序标准,例如:
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);
}
});
【问题讨论】:
-
'this.attributes.events = new feNoRequire.Collections.EventCollection(this.attributes.events);'
-
对于每个模型,我都有一个我想成为模型的事件列表,以便我可以对它们进行排序。
-
一个迟到的评论,当然,但在语义上,我会通过
this.sort()将View.changeSort中对this.collection.sort()的手动调用移动到集合的函数中;这样你就知道当你在集合上调用changeSort()时,它总是会重新排序。
标签: javascript backbone.js backbone-views