admin管理员组文章数量:1429658
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 badges2 Answers
Reset to default 8To 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
本文标签: javascriptHow can I get the index of an item added to a Backbone collection via fetchStack Overflow
版权声明:本文标题:javascript - How can I get the index of an item added to a Backbone collection via fetch? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745420074a2657860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论