问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/24327798/deleting-hasmany-record-and-saving-with-ember-data