【问题标题】:Delete record whose model exists only locally Ember删除模型仅存在于本地 Ember 的记录
【发布时间】:2015-02-23 11:51:13
【问题描述】:

我创建了这条记录,其模型仅存在于本地(不在 api 中)。 模型如下:

TM.Vdpmodel=DS.Model.extend({
type:DS.attr(),
description:DS.attr(),
vds:DS.attr(),    
});

我使用此模型在 JSON 中创建对象数组 (vdps)。见下文。

{"vdps":[{"type":"ssdfdfg","description":"fhfgh","vds":[{"type":"fghgh","example":"jghhgj"},{"type":"gjghj","example":"ghjghj"},{"type":"ghjghj","example":"ghjghj"}]}],"asset_usage":[]}

我使用

创建它们
TM.obj.vdp.pushObject(this.store.createRecord('vdpmodel',{
            type:this.get("vdp11"),
            description:this.get("vdp12"),
            vds:TM.vdobj.vd.objectsAt([this.get("vdcount"),this.get("vdcount")+1,this.get("vdcount")+2])   
})); 

此记录仅存储在本地。现在我想从这个存储中删除一条类型值为“abc”的记录。我该怎么做呢?请记住,该记录目前仅存在于本地。

我尝试了以下方法:

var records=this.store.filter('vdpmodel',function(data){
  return data.get('type')==="abc";
});  
records.forEach(function(record){
  record.deleteRecord();
});         

但永远不会填充记录。知道我在这里做错了什么吗?

【问题讨论】:

    标签: ember.js delete-record


    【解决方案1】:

    我设法通过对 store.all 的结果使用 filterby 来解决这个问题

    var records = this.store.all('vdpmodel'); 
    var filteredRecords=records.filterBy("type","abc");
    filteredRecords.forEach(function(data){
         data.deleteRecord();
    });
    

    【讨论】:

    • 我认为你应该 removeObject 因为记录只存在于本地,不需要与后端同步。 deleteRecord 将导致调用服务器到commit 更改,因为它是本地的,所以不需要调用。
    • 您应该使用 unloadRecord,正如您在上一个问题中提到的那样,stackoverflow.com/questions/28648200/…
    • 我用 uloadRecord 和 deleteRecord 都试过了,似乎都在做同样的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    相关资源
    最近更新 更多