javascript - How can I get the index of an item added to a Backbone collection via fetch? - Stack Overflow

I’ve got a Backbone Collection. I’m using fetch({add:true}) to fetch new items from my server, and add

I’ve got a Backbone Collection. I’m using fetch({add:true}) to fetch new items from my server, and add them to the collection.

I’ve bound a listener function to the collection’s add event. I’d like that function to get the index at which the item was added to the collection.

Backbone’s documentation for Collection.add says “if you're a callback listening to a collection's "add" event, options.index will tell you the index at which the model is being added to the collection.”

I’ve logged the arguments that seem to be passed to my listener function to the console and had a look at them. As far as I can tell, the first argument is the item added, followed by a temporary collection object created to hold it when it came back from the server. I don’t seem to have an object with an index property.

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

I’ve got a Backbone Collection. I’m using fetch({add:true}) to fetch new items from my server, and add them to the collection.

I’ve bound a listener function to the collection’s add event. I’d like that function to get the index at which the item was added to the collection.

Backbone’s documentation for Collection.add says “if you're a callback listening to a collection's "add" event, options.index will tell you the index at which the model is being added to the collection.”

I’ve logged the arguments that seem to be passed to my listener function to the console and had a look at them. As far as I can tell, the first argument is the item added, followed by a temporary collection object created to hold it when it came back from the server. I don’t seem to have an object with an index property.

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

Share Improve this question edited Feb 4, 2013 at 17:41 Paul D. Waite asked Feb 7, 2012 at 13:19 Paul D. WaitePaul D. Waite 99k57 gold badges203 silver badges271 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

To anybody reading this in the future, NOTE: since version 0.9.9, options.index is no longer set. From the changelog:

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

Check the third argument to your bind function, it should contain the 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 options.index is only available in version 0.9

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745420074a4626924.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信