3
votes

I'm trying to load fixtures into my app that has embedded records.

Loading the data from the server (using a DS.RESTAdapter) works, but it does not when I try to load them through the DS.FixtureAdapter.

Models:

App.Post = DS.Model.extend
  title: DS.attr('string')
  comments: DS.hasMany('App.Comment')

App.Comment = DS.Model.extend
  text: DS.attr('string')
  # I'm not specifying DS.belongsTo('post') because comments could also exist 
  # with other objects.
  # Anyway, it does not work even with it. 

Adapter:

App.Adapter = DS.FixtureAdapter.extend()
App.Adapter.map App.Post,
  comments:
    embedded: 'always'

Store:

App.store = DS.Store.create
  revision: 11
  adapter: App.Adapter.create()

Fixtures:

App.Comment.FIXTURES = []

App.Post.FIXTURES = [
  {
    id: "1"
    title: "My post"
    comments: [{
      id: "1"
      text: "My first comment" 
    }, {
      id: "2"
      text: "My second comment"
    }]
  }
]

And in the console:

post = App.store.find(App.Post, 1);

comments = post.get("comments");
console.log(comments.get('length')); // => 1

firstComment = comments.get('firstObject');
console.log(firstComment.get('id')); // => undefined
console.log(firstComment.get('name')); // => TypeError: Cannot call method `hasOwnProperty` of undefined

I think this question is related to this question, and maybe to this pull request.

Ember version used:

EDIT

Here is the JSFiddle that illustrate the problem. And I also wrote a failing test here.

1

1 Answers

2
votes

Hmm, at first I was guessing fixtures without id, but no. Perhaps

App.Adapter = DS.FixtureAdapter.extend()
App.Adapter.map App.Product,
  groups:
    embedded: 'always' 

should contain also App.Post embeds comments always ?

If this does not solve this issue, I suspect the fixture adapter does not ork well with embedded, and you have to define the comments too.

UPDATE: After a discussion with @tomdale, no changes will be put into the fixture adapter in order to make it work loading embedded records. So all I can propose is this:

http://jsfiddle.net/4Zqvc/9/

BTW, here is what exactly tom said:

I do not believe that fixture adapter supports embedded records is there a good reason why it would need to? it also doesn't support underscored_property_names the idea of the fixture adapter is not to mimic the JSON payload from the server it is provide stub data in the format Ember Data expects so relationships are not embedded, property names are camelCased, etc.`