How can I get the index of an item added to a Backbone collection through a selection?

I have a consolidated collection. I use fetch({add:true}) to retrieve new items from my server and add them to the collection.

I linked the listener function to add event collections. Id like this function to get the index at which the item was added to the collection.

The base script documentation for Collection.add says: "If you are a callback listening for the "add" event, options.index tells you the index at which the model is added to the collection."

Ive recorded the arguments that seem to be passed to my console function on the console and looked at them. As far as I can tell, the first argument is the added item, followed by a temporary collection object created to store it when it returns from the server. I don't seem to have an object with the index property.

How can I get the index at which the item was added to the collection?

+6
source share
2 answers

To anyone who reads this in the future, NOTE: since version 0.9.9, options.index is no longer installed. From the change log:

To improve add performance, options.index will no longer be set in the add event callback. collection.indexOf(model) can be used to retrieve the model index as needed.

+8
source

Mark the third argument of your binding function, it should contain an index property

 var c=new Backbone.Collection(); c.bind("add",function(model,collection,opts){ console.log(opts); }); c.add({}); c.add({}); 

Edit: I just checked Backbone 0.5.3 and it would seem that options.index is only available in version 0.9

+2
source

Source: https://habr.com/ru/post/907851/


All Articles