I’m trying to work with embedded records on saving and loading.
My code looks like this:
App.Poll = DS.Model.extend({
title : DS.attr('string'),
options : DS.hasMany('option', {async: true}),
});
App.Option = DS.Model.extend({
title : DS.attr('string')
});
App.PollSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
options: {embedded: 'always'}
}
});
For testing reasons I’m using LSAdapter.
On saving the object to local storage every thing seems fine. The JSON looks like expected:
{
"id": "snf3c",
"title": "Title",
"options": [
{
"title": "OptionA",
"id":"1"
}, {
"title": "OptionB",
"id":"2"
}
]
}
But when loading the data from local storage I’m getting error messages.
First problem was that options did not had an id. I read here https://github.com/emberjs/data/pull/1234#issuecomment-27305845 that embedded data without id isn’t supported yet. So I added IDs for option manually on store.createRecord. That is where the IDs in JSON come from.
But now I get a new error saying:
Error while loading route: TypeError: Cannot read property 'options' of undefined
at null.<anonymous> (file:///home/main/NetBeansProjects/croodle/lib/ember-data.js:9463:19)
at file:///home/main/NetBeansProjects/croodle/lib/ember-data.js:6623:16
at file:///home/main/NetBeansProjects/croodle/lib/ember.js:3165:16
at Object.OrderedSet.forEach (file:///home/main/NetBeansProjects/croodle/lib/ember.js:3008:10)
at Object.Map.forEach (file:///home/main/NetBeansProjects/croodle/lib/ember.js:3163:10)
at Function.DS.Model.reopenClass.eachRelationship (file:///home/main/NetBeansProjects/croodle/lib/ember-data.js:6622:38)
at updatePayloadWithEmbedded (file:///home/main/NetBeansProjects/croodle/lib/ember-data.js:9445:8)
at DS.EmbeddedRecordsMixin.Ember.Mixin.create.extractSingle (file:///home/main/NetBeansProjects/croodle/lib/ember-data.js:9415:5)
at superWrapper [as extractSingle] (file:///home/main/NetBeansProjects/croodle/lib/ember.js:1202:16)
at null.extractFind (file:///home/main/NetBeansProjects/croodle/lib/ember-data.js:87:29)
I have no idea why this error occurs. Is there a bug in EmbeddedRecordsMixin or did I use it wrong?
I’m new to EmberJS, so if I’m totally on the wrong way, please let me know.
Best regards