I'm having problems resorting my collection upon a click event. The sorting function is triggered and executes for each model but neither the reset event is gets triggered nor the collection changes on the view.
I have multiple sort criterias defined on my collection like:
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");
}
});
And on my view file:
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;
}
Any idea on how to solve this?
EDIT After the answers bellow the render is now triggered but the objected only get sorted on initialize. Any subsequent sorts return the collection in the initial order - this.changeSort("name");
my model:
feNoRequire.Models.CompetitionModel = Backbone.Model.extend({
initialize: function(){
this.attributes.events = new feNoRequire.Collections.EventCollection(this.attributes.events);
}
});
this.collection.sort()
in theView.changeSort
into the Collection's function, viathis.sort()
; this way you know when you callchangeSort()
on the collection, it will always re-sort itself. – mix3d