문제

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!

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top