I have a simple backbone.js twitter application that should sort the tweets in reverse order. I am currently using a date sorter. When the Reverse button is pressed (as seen in the view), how can I change the sort order of all tweets without returning through the comparator? My impression is that when I call the sort, it will try to re-display the list (this means that the comparator will sort the data again, which is undesirable). How do I override this?
Tweet = Backbone.Model.extend();
Tweets = Backbone.Collection.extend(
{
model: Tweet,
url: 'http://search.twitter.com/search.json?q=codinghorror',
parse: function(response) {
$.each(response.results, function(i,val) {
val.created_at = val.created_at.slice(0, val.created_at.length - 6);
});
return response.results;
},
sync: function(method, model, options) {
var that = this;
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: that.url,
processData: true
}, options);
return $.ajax(params);
},
comparator: function(activity){
var date = new Date(activity.get('created_at'));
return -date.getTime();
}
});
TweetsView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render');
this.collection = new Tweets;
var that = this;
this.collection.fetch({
success: function (s) {
console.log("fetched", s);
that.render();
}
});
},
el: $('#tweetContainer'),
template: _.template($('#tweettemplate').html()),
render: function() {
$(this.el).html(this.template({ tweets: this.collection.toJSON() }));
},
events : {
'click .refresh' : 'refresh',
**'click .reverse' : 'reverse'**
},
refresh : function() {
this.collection.fetch();
console.log('refresh', this.collection);
this.render();
},
**reverse : function() {**
console.log("you clicked reverse");
console.log(this.collection, "collection");
this.collection.sort();
**}**
});
var app = new TweetsView();
});
source
share