Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all records associated with an ember model without clearing local Storage?

I have extended the program given in this stackoverflow answer to have a program that deletes all the records all at once. However, the deletion happens only in batches and does not delete everything all at once.

Here is a snippet of the function I am using in this JSBin.

deleteAllOrg: function(){
  this.get('store').findAll('org').then(function(record){
    record.forEach(function(rec) {
        console.log("Deleting ", rec.get('id'));
        rec.deleteRecord();
        rec.save();
    });
    record.save();
  });
}

Any idea how the program can be modified such that all records can be deleted at once?

I have also tried model.destroy() and model.invoke('deleteRecords') but they don't work.

Any help is greatly appreciated. Thanks for your help!

like image 730
user2431285 Avatar asked Oct 08 '13 20:10

user2431285


People also ask

What is store in Ember?

One way to think about the store is as a cache of all of the records that have been loaded by your application. If a route or a controller in your app asks for a record, the store can return it immediately if it is in the cache.

What is a model in Ember?

In Ember Data, models are objects that represent the underlying data that your application presents to the user. Note that Ember Data models are a different concept than the model method on Routes, although they share the same name.


1 Answers

Calling deleteRecord() within forEach will break the loop. You can fix it by wrapping the delete code in an Ember.run.once function like this:

  this.get('store').findAll('org').then(function(record){
     record.content.forEach(function(rec) {
        Ember.run.once(this, function() {
           rec.deleteRecord();
           rec.save();
        });
     }, this);
  });

See this jsBin.

like image 109
chopper Avatar answered Oct 13 '22 12:10

chopper