删除具有多个记录并使用ember数据进行保存 [英] Deleting hasMany record and saving with ember-data

查看:98
本文介绍了删除具有多个记录并使用ember数据进行保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



http://emberjs.jsbin.com/sovub/3/edit



从我的例子中,在某些情况下,当我尝试删除一个字幕,然后保存,我得到错误:

 尝试处理事件`pushData`在< App.Subtitle:ember563 :7个而在状态root.deleted.uncommitted。 

如果我删除最后一个字幕,然后保存,没关系。但是删除第一个字幕,或删除和添加新记录,然后保存给我错误消息。



是因为我手动设置每个字幕的ID在 extractSingle ,像这样:

  extractSingle:function(store,type,payload ,id){
var list = payload.list
var nid = 6

//额外字幕
var subs = list.subtitles
列表。 subtitles = []

subs.forEach(function(item){
item.id = nid ++
list.subtitles.push(item.id)
item。 list = list.id
})
//对链接执行相同

payload = {list:list,subtitle:subs,link:li}
return this._super(store,type,payload,id)
},

还注意到,每当抛出错误时, extractSingle 中的有效负载的字幕属性不包含正确的模型。相反,它只包含字幕记录的ID。

  //通常
id:532,
字幕:Array [2]
0:Class
__ember1403240151252:ember562
__ember1403240151252_meta:Object
//剩余的数据

1 :class
__ember1403240151252:ember563
__ember1403240151252_meta:Object
__nextSuper:undefined
//剩余的数据

//抛出错误
id:532,
subtitles:Array [1]
0:6
length:1
__proto__:Array [0]

我不太确定我应该如何处理这个,更不用说解决它了。任何帮助将不胜感激。

解决方案

我做了更多的研究,发现关于 DS.RootState http://emberjs.com/api/data/ classes / DS.RootState.html ),它与状态root.deleted.uncommitted 错误相关。



为了解决这个问题,我所做的一切都是在删除记录之后取消实现的:

  var model = this。 get('model')

model.deleteRecord()
this.store.dematerializeRecord(model)

这将删除记录索引(和关系?),以便正确删除。


I have a jsbin setup with my issue:

http://emberjs.jsbin.com/sovub/3/edit

From my example, in certain situations when I try to delete a subtitle and then save, I get the error:

Attempted to handle event `pushedData` on <App.Subtitle:ember563:7> while in state root.deleted.uncommitted.

If I delete the last subtitle then save, it's fine. But deleting the first subtitle, or deleting and adding new records then saving gives me the error message.

Is it because I'm manually setting the IDs for each subtitle during extractSingle, like so:

extractSingle: function(store, type, payload, id){
    var list = payload.list
    var nid = 6

    // extra subtitles
    var subs = list.subtitles
    list.subtitles = []

    subs.forEach(function(item){
        item.id = nid++
        list.subtitles.push(item.id)
        item.list = list.id
    })
    // do the same for links

    payload = { list: list, subtitle: subs, link: li}
    return this._super(store, type, payload, id)
},

I've also noticed that the subtitles attribute of the payload in extractSingle doesn't contain the correct model whenever the error is thrown. Instead, it contains only the ID of the subtitle record.

// normally
id: "532",
subtitles: Array[2]
    0: Class
    __ember1403240151252: "ember562"
    __ember1403240151252_meta: Object
    // rest of data

    1: Class
    __ember1403240151252: "ember563"
    __ember1403240151252_meta: Object
    __nextSuper: undefined
    // rest of data

// when error is thrown
id: "532",
subtitles: Array[1]
    0: 6
    length: 1
    __proto__: Array[0]

I'm not really sure as how I should approach this, let alone resolve it. Any help would be appreciated.

解决方案

I did some more research and found out about DS.RootState ( http://emberjs.com/api/data/classes/DS.RootState.html) which is related to the state root.deleted.uncommitted error I was getting.

To solve it, all I did was dematerialize the record after deleting it:

var model = this.get('model')

model.deleteRecord()
this.store.dematerializeRecord(model)

This removed the records indexes (and relationships?) so it was properly deleted.

这篇关于删除具有多个记录并使用ember数据进行保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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