Question

I'm using backbone.js for my web app and it works semi-well so far. The problem is that sometimes I need to add items to the collection and have them go to the top of the page, and other times to the bottom. Sometimes I need them to animate, sometimes not. Is there a way to do this cleanly with backbone.js? Passing arguments to the "add" event would be a good way (for example "prepend: true"), but this doesn't seem to be an option...

Thanks!

Was it helpful?

Solution

Extend Backbone.Collection and override the add function:

PositionCollection = Backbone.Collection.extend ({
  add: function( model, options ){
    // do your stuff
    // call the real add
    Backbone.Collection.prototype.add.call(this, model);
  }
});

Only thing to keep in mind is that if you prepend, append you might want to copy Backbone.Collection.add altogether and play with it.

You could also rely on the collection sorting. Add your position and animation attribute to the model, sort on the position and listen to "add" events on the collection to animate it correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top