Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7878267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:36:37+00:00 2026-06-03T03:36:37+00:00

I have a simple backbone.js twitter application that needs to sort tweets in reverse

  • 0

I have a simple backbone.js twitter application that needs to sort tweets in reverse order. I’ve currently implemented a comparator sort by date. When the “Reverse” button is clicked (as seen in the View) how do I reverse sort all the tweets without going back through the comparator? My impression is that when I call sort it will attempt to re-render the list (which means the comparator will sort the data again, which is undesirable). How do I override this?

Tweet = Backbone.Model.extend();

 // Define the collection
Tweets = Backbone.Collection.extend(
{
    model: Tweet,
    // Url to request when fetch() is called
    url: 'http://search.twitter.com/search.json?q=codinghorror',

    parse: function(response) {

        //modify dates to be more readable
        $.each(response.results, function(i,val) {
            val.created_at = val.created_at.slice(0, val.created_at.length - 6);
          });

        return response.results;
    },
    // Overwrite the sync method to pass over the Same Origin Policy
    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();

    }
});

   // Define the View
  TweetsView = Backbone.View.extend({
initialize: function() {
  _.bindAll(this, 'render');
  // create a collection
  this.collection = new Tweets;
  // Fetch the collection and call render() method
  var that = this;
  this.collection.fetch({
    success: function (s) {
        console.log("fetched", s);
        that.render();
    }
  });
},

el: $('#tweetContainer'),
// Use an external template

template: _.template($('#tweettemplate').html()),

render: function() {
    // Fill the html with the template and the collection
    $(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();

   //How do I reverse the list without going through the comparator?

**}**

});

var app = new TweetsView();
 });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:36:38+00:00Added an answer on June 3, 2026 at 3:36 am

    The usual solution to Backbone problems is to use events. Calling sort will trigger a "reset" event:

    Calling sort triggers the collection’s "reset" event, unless silenced by passing {silent: true}.

    So you could have a “sort order” flag in your collection:

    Backbone.Collection.extend({
        //...
        initialize: function() {
            //...
            this.sort_order = 'desc';
            //...
        }
    });
    

    and then your comparator can pay attention to that flag:

    comparator: function(activity) {
        var date = new Date(activity.get('created_at'));
        return this.sort_order == 'desc'
             ? -date.getTime()
             :  date.getTime()
    }
    

    and you could have a method on the collection to change the sort order:

    reverse: function() {
        this.sort_order = this.sort_order = 'desc' ? 'asc' : 'desc';
        this.sort();
    }
    

    Then your view can listen for the "reset" event and redisplay the collection when you change the sort order. Once all that’s in place, you just tell your your reverse button to call view.collection.reverse() and everything will be fine.

    Demo: http://jsfiddle.net/ambiguous/SJDKy/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple form that passes data to backbone, which in turn, submits
I have a simple comparator function on a Backbone.js collection. comparator: function (topic) {
I have implemented a simple close() method for all the Backbone views which disposes
New to using Backbone and have a very simple application. Basically there are Clients
I'm working on a backbone rails application that needs to poll the server to
I have a super simple Backbone model/collection that wraps around a facebook feed. window.Story
I have simple win service, that executes few tasks periodically. How should I pass
I have a relatively simple Backbone handled message container. I can add messages, and
I have simple user model: var user = Backbone.Model.extend({ initialize: function(){ this.bind(change:auth, function (){
I have noticed that when multiple attributes of a Backbone model are set like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.