如何在不清除本地存储的情况下删除与 ember 模型关联的所有记录? [英] How to delete all records associated with an ember model without clearing local Storage?

查看:16
本文介绍了如何在不清除本地存储的情况下删除与 ember 模型关联的所有记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我扩展了这个 stackoverflow answer 有一个程序可以一次删除所有记录.但是,删除只会分批进行,并不会一次性删除所有内容.

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.

这是我在这个 JSBin 中使用的函数的片段.

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?

我也尝试过 model.destroy() 和 model.invoke('deleteRecords') 但它们不起作用.

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

非常感谢任何帮助.感谢您的帮助!

Any help is greatly appreciated. Thanks for your help!

推荐答案

forEach 中调用 deleteRecord() 将打破循环.您可以通过将删除代码包装在 Ember.run.once 函数中来修复它,如下所示:

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);
  });

参见这个jsBin.

这篇关于如何在不清除本地存储的情况下删除与 ember 模型关联的所有记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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