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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:16:12+00:00 2026-06-05T10:16:12+00:00

I have a collection which sorts itself based on a model’s attribute, sometimes I

  • 0

I have a collection which sorts itself based on a model’s attribute, sometimes I want to add a model and just calculate the order within the add event handler. The problem is that when I call collection.sort() within the add event handler, for some reason it fires the add event again.

Since my add event does some DOM inserts, I end up getting duplicate items in my DOM.

So far the only solution I have found is to calculate the next order before adding the model or calling _.defer and sorting the collection then running any further code

See full fiddle: http://jsfiddle.net/DD23n/9/

  • 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-05T10:16:15+00:00Added an answer on June 5, 2026 at 10:16 am

    sntran is right about where your problem is coming from: you’re modifying an array while you’re iterating over it.

    You start off with your new model with nOrder: null at the beginning of the model list:

    splice.apply(this.models, [index, 0].concat(models));
    

    then add loops through the models triggering 'add' events as it goes:

    for (i = 0, length = this.models.length; i < length; i++) {
      if (!cids[(model = this.models[i]).cid]) continue;
      options.index = i;
      model.trigger('add', model, this, options);
    }
    

    But inside your 'add' callback, you modify the model:

    if(model.get('nOrder') == null)
        model.set('nOrder', _.max(collection.pluck('nOrder')) + 1);
    

    and then sort the collection:

    collection.sort({silent: true});
    

    These two actions move this.models[0] to this.models[3] in the event triggering loop; but the i for that loop will just keep ticking along and the new this.models[3] (which used to be at 0) will get past the if (!cids[(model = this.models[i]).cid]) test again and there’s your second 'add' event.

    You can watch this version of your fiddle work to see how the collection’s array is changing behind your back:

    http://jsfiddle.net/ambiguous/p8Fp4/

    I think the simplest solution is to add an append method to your collection that sets the appropriate nOrder value on the model and then adds it to the collection:

    append: function(m) {
        var nOrder = _.max(this.pluck('nOrder')) + 1;
        if(m instanceof Backbone.Model)
            m.set({ nOrder: nOrder });
        else
            m.nOrder = nOrder;
        this.add(m);
    }
    

    Then your 'add' callback can leave nOrder alone and stop sorting the collection.

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

    You could also override the collection’s add method but that is a lot more complicated if you want to do it right.

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

Sidebar

Related Questions

We are currently implementing a solution where we have a collection which we want
I have a collection (or list or array list) in which I want to
I have written a comparator which sorts by ascending order as below. which is
I have an observable collection which is binded to datagrid... I want to sort
In my application, I have three collection objects which store data. The data which
I have a collection of objects which describe an image-name, its size and it's
I have a collection of arrays which are generated dynamically. What I am trying
Let's say I have a collection of objects which can be sorted using a
Hi I am trying to return a collection which may potentially have null date
I have a collection of static libraries (.lib) files one of which may have

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.