i'm attempting bulk delete collection of backbone models so...
collection.each(function(model, i){ model.destroy(); });
i'm finding when each loop contains model.destroy(), stops after count of 10. if run again, stops 5. times after 3.. 2.. , 1.
if replace model.destroy() console.log(i), loop runs length of collection.
is intentional limitation within backbone keep deleting 1000 records in 1 shot or browser limitation on number of relatively simultaneous delete methods?
is intentional limitation within backbone keep deleting 1000 records in 1 shot or browser limitation on number of relatively simultaneous delete methods?
no.
the problem munging collection on each iteration. consider following.
say start off collection of 10 models:
// pretend collection has 10 models [m0,m1,m2,m3,m4,m5,m6,m7,m8,m9] collection.each(function(model,i) { model.destroy(); });
on first iteration, collection.length === 10
, arguments collection.each
m0
, 0
. call, m0.destroy()
lives @ index 0.
at end of first iteration collection
contains following models
[m1,m2,m3,m4,m5,m6,m7,m8,m9]
the following problem starts:
now second iteration collection.length === 9
. each
function on second iteration , grabbing model @ index of 1, , that's model 2 lives. arguments each
m2,1
. call m2.destroy()
, remove collection.
at end of second iteration collection contains following:
[m1,m3,m4,m5,m6,m7,m8,m9]
the third iteration, index 2 , m4.destroy()
called. leaving with:
[m1,m3,m5,m6,m7,m8,m9].
this happens until index greater collection.length
, stops. leaving unwanted models in collection.
the following should work you:
while ( (model=collection.shift()) ) { model.destroy() }