Best Practice for moving backbone model within a collection
This is sort of related to this question - BackboneJS best way to
rearrange models in a collection while maintaining 0-indexed ordinal
property for each model
I have a collection, visually represented in a list. This list is
drag/droppable. Any item can be moved to an arbitrary position in the
collection (ie - not a sort). I've seen some examples that use the
collection's native remove/add to put the model in the right place.
However, Backbone internally calls set when models are added, which then
calls a bunch of methods related to events and sorts it at the end. Is
there any downside to just splicing the model to the correct position?
Remove/Add:
See examples in linked question.
Splice:
moveTo: function(oldIndex, newIndex){
oldIndex = oldIndex instanceof Backbone.Model ? this.at(oldIndex)
: oldIndex;
var spliced = this.models.splice(oldIndex, 1);
this.models.splice(newIndex, 0, spliced[0]);
this.trigger("move",[oldIndex,newIndex]);
},
No comments:
Post a Comment