2

I am having trouble loading a model instance in ember js with ember-data. An exception is thrown when using the function store.find . Instances are created with the FixtureAdapter. The problem occurs when hasMany relations are defined in the model.

App = Ember.Application.create();

App.Store = DS.Store.extend({
    adapter: DS.FixtureAdapter
});

App.Country = DS.Model.extend({
    'name': DS.attr('string'),
    'cities': DS.hasMany('city', {embedded: true})
});

App.City = DS.Model.extend({
    'name': DS.attr('string'),
    'country': DS.belongsTo('country')
});

App.City.FIXTURES = [{
}];

App.Country.FIXTURES = [{
    'id': 1,
    'name': 'USA',
    'cities': [{id: 1, name: 'New York'}, {id: 2, name: 'San Francisco'}]
},{
    'id': 2,
    'name': 'Kanada',
    'cities': [{id: 3, name: 'Montreal'}]
}];

App.IndexRoute = Ember.Route.extend({
    model: function() {
        var store = this.store;
        var country = store.find('country', 1);
        return country;
    }
});

http://jsfiddle.net/xSq4y/

DEBUG: ------------------------------- ember.js:3496
DEBUG: Ember      : 1.5.0-beta.2 ember.js:3496
DEBUG: Ember Data : 1.0.0-beta.7.f87cba88 ember.js:3496
DEBUG: Handlebars : 1.3.0 ember.js:3496
DEBUG: jQuery     : 2.1.0 ember.js:3496
DEBUG: ------------------------------- ember.js:3496
Error while loading route: TypeError: Cannot set property 'store' of undefined
    at Ember.Object.extend.modelFor (http://builds.emberjs.com/tags/v1.0.0-beta.7/ember-data.js:9812:23)
    at Ember.Object.extend.recordForId (http://builds.emberjs.com/tags/v1.0.0-beta.7/ember-data.js:9265:21)
    at deserializeRecordId (http://builds.emberjs.com/tags/v1.0.0-beta.7/ember-data.js:10196:27)
    at deserializeRecordIds (http://builds.emberjs.com/tags/v1.0.0-beta.7/ember-data.js:10210:9)
    at http://builds.emberjs.com/tags/v1.0.0-beta.7/ember-data.js:10176:11
    at http://builds.emberjs.com/tags/v1.0.0-beta.7/ember-data.js:8517:20
    at http://builds.emberjs.com/tags/v1.5.0-beta.2/ember.js:3403:16
    at Object.OrderedSet.forEach (http://builds.emberjs.com/tags/v1.5.0-beta.2/ember.js:3246:10)
    at Object.Map.forEach (http://builds.emberjs.com/tags/v1.5.0-beta.2/ember.js:3401:10)
    at Function.Model.reopenClass.eachRelationship (http://builds.emberjs.com/tags/v1.0.0-beta.7/ember-data.js:8516:42) 
4
  • 1
    hasMany fixtures should be defined like this imo "id1: cities : [1,2]" Commented Feb 28, 2014 at 12:41
  • thx, it works like that. I'm still wondering why embedded syntax cannot be used. Commented Feb 28, 2014 at 13:40
  • isnt this the point of having primary keys ?=) Commented Mar 1, 2014 at 19:42
  • How were you able to tell that the issue was related to the cities field from the provided traceback? Commented May 20, 2014 at 22:01

1 Answer 1

0

If your API is returning data in that format you can use a serializer to convert it to the format that Ember Data is expecting. Try this:

App.CountrySerializer = DS.RESTSerializer.extend({
  normalizePayload: function(type, payload) {
    var tempCities = payload.country.cities;
    payload.country.cities = [];
    tempCities.forEach(function(city) {
      payload.country.cities.push(city.id);
    });
    payload.cities = tempCities;
    return this._super(type, payload);
  },
})
Sign up to request clarification or add additional context in comments.

2 Comments

For the moment, I'd like to use fixtures without a server connection. Is it possible to realize that with the RESTSerializer?
I've never used fixtures, but the serializer doesn't care where the data came from. You should be able to use a FixtureAdapter to fetch the data and still use a sub-class of RESTSerializer to process it. In any event you can override the normalizePayload function of which ever serializer you are using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.