Reputation: 882
Lost.Router.map(function() {
this.resource('countries',{path: '/'}, function(){
this.resource('country',{path: ':country_id'},function(){
this.resource('city',{path: ':city_id'});
});
});
});
My country model is
Lost.Country = DS.Model.extend({
countryCode: DS.attr('string'),
countryName: DS.attr('string'),
places: DS.hasMany('place')
});
and my place model is
Lost.Place = DS.Model.extend({
cityName: DS.attr('string'),
country: DS.belongsTo('country')
});
In country model when I change places to place this work fine but when i keep it as places I get error
TypeError: Cannot set property 'store' of undefined
DEBUG: Ember : 1.6.0-beta.1+canary.3bcd9bdc
DEBUG: Ember Data : 1.0.0-beta.7+canary.d5562867
DEBUG: Handlebars : 1.3.0
DEBUG: jQuery : 1.11.0
UPDATED
The object returned from rails server
Acual JSON:
{
"countries":[
{
"id":1,
"countryCode":"BH",
"countryName":"Bhutan",
"places":[
{
"id":1,
"country_id":1,
"cityName":"Daga"
},
{
"id":2,
"country_id":1,
"cityName":"Ha"
}
]
}
]
}
Here is how my rails serializers looks
application_serializer.rb
class ApplicationSerializer < ActiveModel::Serializer
embed :ids, :include => true
end
country_serializer.rb
class CountrySerializer < ActiveModel::Serializer
attributes :id, :countryCode, :countryName
has_many :places
end
place_serializer
class PlaceSerializer < ActiveModel::Serializer
attributes :id, :country_id, :cityName
#belongs_to :country #commented out because gives error"undefine method belongs_to"
#if i change it to has_one gives error"stack level too deep"
end
* *Additon to anwer ** this question has been answered by @kingpin2k as a part of answer if you are new developer like me instead of writing your own normalizeAttribute function I think it will be better to use already exsisting normalizeAtrribute function in DS.RESTAdapter present in ember-data. less error prone. Just copy pasting it worked for me
Ember Data : 1.0.0-beta.6
Upvotes: 1
Views: 387
Reputation: 47367
Lost.Place = DS.Model.extend({
cityName: DS.attr('string'),
country: DS.belongsTo('Country')
});
should be (lower case country)
Lost.Place = DS.Model.extend({
cityName: DS.attr('string'),
country: DS.belongsTo('country')
});
and I can't tell from your question, but the json should be an array of ids for place.
If you're using the ActiveModelSerializer
then you can set the embedded to always, which is how you're trying to return it. You're data is causing some hiccups, in that it sees countryCode
as country_code
, but you can override some functions to fix up this validation, or you can fix up your json to match the expected data.
App.ApplicationAdapter= DS.ActiveModelAdapter;
App.ApplicationSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
places: {embedded: 'always'}
},
normalizeAttributes: function(type, hash) {
//your attributes shouldn't be normalized
}
});
Lost.Country = DS.Model.extend({
countryCode: DS.attr('string'),
countryName: DS.attr('string'),
places: DS.hasMany('place')
});
http://emberjs.jsbin.com/OxIDiVU/342/edit
Upvotes: 1