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?
Try 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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With