[英]backbone.js empty a collection
我需要清空一个集合,按顺序删除每个项目。
this.nodes.each(function(node){
this.nodes.remove(node);
}, this);
不起作用,因为随着每个节点被删除,它会更改集合的长度。 制作一个临时数组,然后迭代该数组。 有没有更好的办法?
尝试this.nodes.reset()
除非你需要remove
事件。
除此以外:
var nodes = this.nodes;
while (nodes.length > 0)
nodes.remove(nodes.at(0));
清空骨干集合的另一种方法:
while ( this.catz.length > 0) this.catz.pop();
如果您需要在迭代时修改集合,那么使用一个简单的反向操作for
完成它:
var count = collection.size();
for (var i = count-1; i > -1; i--) {
collection.remove(collection.at(i));
}
http://backbonejs.org/#Collection-reset
你可以调用collection.reset();
它会清空整个集合。 我今天用了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.