S
S
SvetlyiAkaPro2015-03-07 15:36:46
JavaScript
SvetlyiAkaPro, 2015-03-07 15:36:46

What's wrong with removing models from the Backbone collection?

I can not delete all models from the collection, they are only partially deleted. Stored in LocalStorage. I delete like this:

_.each(myApp.Animals.models, function(animal) {
  animal.destroy();
});

Several elements are removed and a "cannot read property destroy of undefined" error is thrown. The same with
myApp.Animals.invoke('destroy');
Although this code works fine, everything is rendered
_.each(myApp.Animals.models, function(animal) {
  var animalView = new myApp.AnimalInMyListView({'model': animal});
  self.$el.find('table').append(animalView.render().el);
});

I create like this:
addAnimal: function(animal) {
  var newAnimal = animal.clone();
  newAnimal.set({amount: 1});
  myApp.Animals.create(newAnimal);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SvetlyiAkaPro, 2015-03-07
@SvetlyiAkaPro

Decided like this:

_.each(myApp.Animals.models, function(animal) {
  myApp.Animals.models[0].destroy();
});

It turns out that the array was shifted after deletion, that's why it didn't work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question