Experiencing some issues with sorting and displaying data using backbone.js. The collection is sorted by 'title' in the comparator function, but when rendering begins, the views of models are displayed in a different order.
var TodoList = Backbone.Collection.extend({
model: Todo,
comparator: function(todo) {
return todo.get('title');
},
//function for sorting
sortByDate: function () {
this.comparator = function(todo){
return todo.get('title');
};
this.sort();
}
});
var TodoView = Backbone.View.extend({
tagName: "li",
template: _.template($('#item-template').html()),
initialize: function() {
this.listenTo(this.model, 'change', this.render);
this.listenTo(this.model, 'destroy', this.remove);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});