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

大憨熊 提交于 2019-12-01 02:52:29

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/19257758/how-to-delete-all-records-associated-with-an-ember-model-without-clearing-local

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!