骨干model.destroy()的限制 [英] Backbone model.destroy() limited

查看:219
本文介绍了骨干model.destroy()的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图批量删除骨干车型的集合,像这样......

I'm attempting to bulk delete a collection of backbone models like so...

collection.each(function(model, i){
  model.destroy();
});

我发现当每个循环包含model.destroy(),它的10计数后,如果我再次运行它停止,它停止一个5次后3。2 ..然后1

I'm finding that when the each loop contains model.destroy(), it stops after a count of 10. If I run it again, it stops a 5. The times after 3.. 2.. and then 1.

如果我更换model.destroy()用的console.log(i)中,循环运行集合的长度

If I replace the model.destroy() with console.log(i), the loop runs the length of the collection.

这是一种有意的限制主干内让你从删除的相对同步DELETE方法数1000条记录一次性或浏览器限制?

Is this an intentional limitation within Backbone to keep you from deleting 1000 records in one shot or a browser limitation on the number of relatively simultaneous DELETE methods?

推荐答案

这是骨干中故意限制,让你从删除的相对同步的数量1000条记录一次性或浏览器限制DELETE方法?

没有。

问题是你正在改写(munging)在每次迭代的集合。考虑下。

The problem is you are munging the collection on each iteration. Consider the following.

假设你有10款车型的集合开始:

Say you start off with a collection of 10 models:

// pretend this collection has 10 models [m0,m1,m2,m3,m4,m5,m6,m7,m8,m9]
collection.each(function(model,i) {
  model.destroy();
});

在你的第一次迭代中,你的 collection.length === 10 和参数 collection.each 将为 M0 0 。所以你打电话, m0.destroy()其中住在索引0。

On your first iteration, your collection.length === 10 and the arguments to collection.each will be m0 and 0. So you call, m0.destroy() which lives at index 0.

在第一次迭代的包含以下型号的结尾

At the end of the first iteration your collection contains the following models

[m1,m2,m3,m4,m5,m6,m7,m8,m9]

以下是这个问题开始其中:

The following is where the problem starts:

现在你的第二个迭代您的 collection.length === 9 。在每个函数现在在它的第二个迭代,抓住在1指数模型,而这也正是模式2的生活。所以参数到每个 m2,1 。然后调用 m2.destroy(),并从集合中删除了。

Now for your second iteration your collection.length === 9 . The each function is now on its second iteration and grabbing the model at index of 1, and that's where model 2 lives. So the arguments into each are m2,1. You then call m2.destroy() and remove that from the collection.

在第二次迭代结束您的收藏包含以下内容:

At the end of the second iteration your collection contains the following:

[m1,m3,m4,m5,m6,m7,m8,m9]

第三次迭代,你的指数将2和 m4.destroy()将被调用。留给你:

 [m1,m3,m5,m6,m7,m8,m9].

这会发生,直到指数大于 collection.length 更大,然后停止。不想要的车型让你的收藏。

This happens until the index is greater than collection.length and then stops. Leaving you with unwanted models in your collection.

以下应为你工作:

while ( (model=collection.shift()) ) {
  model.destroy()
}

这篇关于骨干model.destroy()的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆