admin管理员组文章数量:1360185
I need to empty a collection, removing each item in order.
this.nodes.each(function(node){
this.nodes.remove(node);
}, this);
Doesn't work, because as each node is removed it changes the length of the collection. Making a temporary array and then iterating over that works. Is there a better way?
I need to empty a collection, removing each item in order.
this.nodes.each(function(node){
this.nodes.remove(node);
}, this);
Doesn't work, because as each node is removed it changes the length of the collection. Making a temporary array and then iterating over that works. Is there a better way?
Share Improve this question edited Oct 17, 2012 at 21:12 forresto asked Oct 17, 2012 at 14:45 forrestoforresto 12.4k8 gold badges48 silver badges65 bronze badges4 Answers
Reset to default 4Try this.nodes.reset()
unless you need remove
event.
Otherwise:
var nodes = this.nodes;
while (nodes.length > 0)
nodes.remove(nodes.at(0));
Another way to empty of backbone collection:
while ( this.catz.length > 0) this.catz.pop();
If you need to modify collection while iterating, then do it using a simple backward for
like that:
var count = collection.size();
for (var i = count-1; i > -1; i--) {
collection.remove(collection.at(i));
}
Fiddle at http://jsfiddle/xt635/
http://backbonejs/#Collection-reset
You can call collection.reset();
and it'll empty the entire collection. I used it today!
本文标签: javascriptbackbonejs empty a collectionStack Overflow
版权声明:本文标题:javascript - backbone.js empty a collection - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743890131a2556765.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论