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 7820385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:16:08+00:00 2026-06-02T07:16:08+00:00

In my app I have a socket.io connection that is listening to the backend

  • 0

In my app I have a socket.io connection that is listening to the backend and getting updates to models held by the clients browser (which retrieves the model by id and calls set on the model attribute).

I’d like the collection to be sorted, then re-rendered as a whole in order to reflect any new ordering on the models as a result of the set (most examples seem to be around individual views being re-rendered). What’s a method of achieving this?

NB
I’ve got a backbone.js layout lifted pretty verbatim from the example todo app (this is the first backbone app).

  • 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-02T07:16:10+00:00Added an answer on June 2, 2026 at 7:16 am

    You can achieve what you want by providing a comparator method for your collection.

    Example:

    ModelCollection = Backbone.Collection.extend({
        comparator: function(a, b) {
            if ( a.get("name") > b.get("name") ) return 1;
            if ( a.get("name") < b.get("name") ) return -1;
            if ( a.get("name") === b.get("name") ) return 0;
        },
    
        initialize: function() {
            this.on('change:name', function() { this.sort() }, this);
        }
    });
    

    The comparator in this example will cause your collection to be sorted in ascending order by the name attribute of the models inside.

    Note that your collection won’t be sorted automatically when changing attribute(s) of any of its models. By default, sorting happens only when creating new models and adding them to the collection; but the comparator will be used by the collection.sort method.

    The code above takes advantage of this by setting an event listener that simply re-sorts the collection on any changes to the name attributes of its models.

    To complete the picture, we set up an appropriate event listener in the View associated with the collection to make sure it re-renders on any changes:

    CollectionView = Backbone.View.extend({
        initialize: function() {
            this.collection = new ModelCollection();
            this.collection.on('all', function() { this.render() }, this);
        },
    
        render: function() {
            this.$el.html(this.collection.toJSON());
        }
    });
    

    That’s it 🙂


    Relevant excerpt from the Backbone documentation:

    By default there is no comparator for a collection. If you define a comparator, it will be used to maintain the collection in sorted order. This means that as models are added, they are inserted at the correct index in collection.models. A comparator can be defined as a sortBy (pass a function that takes a single argument), as a sort (pass a comparator function that expects two arguments), or as a string indicating the attribute to sort by. […] Collections with a comparator will not automatically re-sort if you later change model attributes, so you may wish to call sort after changing model attributes that would affect the order.

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

Sidebar

Related Questions

I have an Android app that communicates with a server via asynchronous socket connection
I currently have a mobile app that uses a socket connection to communicate with
I have a simple app that listens to a socket connection. Whenever certain chunks
I have an android app which uses camera. I am using a socket connection
I have Android client app that communicates with server using Socket . On my
Say I want to have a server that can accept socket connection and also
I'm having trouble getting a socket connection between Android(client) and a c# app(server) to
I have an app in which I'm trying to detect WHEN the Internet connection
I have an android application with a background thread that contains a socket connection
I am building an app that uses a async socket connection with a web

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.