I am uploading files asynchronously (using FormData) at specific index in the list of files with:
files.create({position: index + 1 }, {at: index}); // files is Backbone.Collection
The server then saves upload and shifts positions for the files after specific position to free the place for the new inserted file.
Then on client I listen to add event and use index from options to insert file view.
files.on("add", function(model, collection, options) {
// insert file view at options.index position
})
I also update position
attribute for all models in collection:
files.on("add remove", function(){
this.each(function(m, index) {
m.set({position: index + 1});
})
});
The problem is when I upload many files at one time at the same index position, file views append to the list in the wrong order.
How to ensure correct order and position
attribute for file models in backbone collection?