mardi 23 décembre 2014

Ember data embedded has one relationship for ember-data.1.0.0

I am trying to serialize a user object and pass it to an ember client.


The user model has_one address model. There is no separate endpoint for just the address model so I can't just provide a foreign key id and sideload it.


The JSON received just includes address as an object and looks something like this:



{"user":
{
"id":"2e168076-0bcb-4418-a183-aeb4600cbc46",
"name":"Andrew,
"address":
{
"id":"5e4af533-23f5-491b-98a8-fe8441c040c3",
"addressable_id":"3",
"street":"1,
"country_code":"US"
}
}


On the ember side I have a user model



App.User = DS.Model.extend({
name: DS.attr('string'),

//hasOne via belongsTo as suggested by another SO post:
//http://ift.tt/1xe8hDO
address: DS.belongsTo('address'),
});


and an address model



App.Address = DS.Model.extend({
addressable_id: DS.attr('string'),
street: DS.attr('string'),
country_code: DS.attr('string'),

user: DS.belongsTo('user')
});


Currently running this code throw an error in the console:



TypeError: Cannot read property 'typeKey' of undefined


which can be fixed by removing the



address: DS.belongsTo('address'),


line in the user model but then the relationship doesn't load properly.


Any help configuring this relationship would be greatly appreciated since it seems like any docs I find are outdated due to being more than an hour old.





Aucun commentaire:

Enregistrer un commentaire