摧毁骨干集合每个型号干净的方式? [英] Cleanest way to destroy every Model in a Collection in Backbone?
问题描述
在我写的第一次尝试
this.collection.each(function(element){
element.destroy();
});
这是不行的,因为它是在Java中,每一个其他元素被删除类似于 ConcurrentModificationException的
。
This does not work, because it's similar to ConcurrentModificationException
in Java where every other elements are removed.
我想结合的模式删除事件,以毁灭自己的建议<一href=\"http://stackoverflow.com/questions/6280553/destroying-a-backbone-model-in-a-collection-in-one-step\">Destroying在集合的骨干示范一步到位?,但这将火2的删除请求,如果我呼吁摧毁属于集合的典范。
I tried binding "remove" event at the model to destroy itself as suggested Destroying a Backbone Model in a Collection in one step?, but this will fire 2 delete requests if I call destroy on a model that belongs to a collection.
我看着下划线doc和不能看到循环倒退每()
的变体,这将解决取出的每一个元素的问题。
I looked at underscore doc and can't see a each()
variant that loops backwards, which would solve the removing every element problem.
您有什么建议,以破坏模型的集合,最彻底的方法?
What would you suggest as the cleanest way to destroy a collection of models?
感谢
推荐答案
您也可以使用一个良好的,ol'派弹出破坏就地:
You could also use a good, ol'-fashioned pop destroy-in-place:
var model;
while (model = this.collection.first()) {
model.destroy();
}
这篇关于摧毁骨干集合每个型号干净的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!