【问题标题】:How to delete all records associated with an ember model without clearing local Storage?如何在不清除本地存储的情况下删除与 ember 模型关联的所有记录?
【发布时间】:2013-10-08 20:31:41
【问题描述】:

我已经扩展了stackoverflow answer 中给出的程序,使其具有一次删除所有记录的程序。但是,删除仅分批进行,不会一次全部删除。

这是我在 JSBin 中使用的函数的 sn-p。

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

知道如何修改程序以便一次删除所有记录吗?

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

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

【问题讨论】:

    标签: ember.js local-storage ember-data ember-model


    【解决方案1】:

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

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

    this jsBin

    【讨论】:

    • 非常感谢!完美运行! :)
    • 现在您可以致电this.get('store').findAll('org').invoke('destroyRecord') 将其全部删除。
    • @Jacob,这实际上是行不通的。 Invoke 使用相同的 forEach 函数。
    • @NicholasJohn16:你是对的,你需要将coalesceFindRequests: true 添加到 RESTAdapter 以让它工作。见:emberjs.com/blog/2014/08/18/…
    • @JacobvanLingen this.get('store').findAll('org').invoke('destroyRecord') 向 /orgs 发送 GET 请求而不是 DELETE
    猜你喜欢
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多