Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

failed to get embedded's object property using ember.js with ember-data

I'm new to ember, and try to understand how it works.

I've defined a store with a fixturesAdapter as adapter (rev 7). I've defined two models:

App.Tag = DS.Model.extend({
    name: DS.attr('string'),
    item: DS.belongsTo('App.Item')
});

and:

App.Item = DS.Model.extend({
    name: DS.attr('string'),
    tags: DS.hasMany(App.Tag, { embedded:true }),
})

I also fill their associated fixtures and at last a controller:

App.itemsController = Ember.ArrayController.create({
    content: App.store.findAll(App.Item)
});

I've defined a function inside App.Item model:

tagline: function(){
    return this.get('tags').toArray().map(function(tag){
        return tag.get('name');
    }).join(',');
}.property('[email protected]')

Here is the corresponding jsfiddle: http://jsfiddle.net/K286Q/29/

My questions are:

  • What am I doing wrong?
  • Why does it see several tags associated to first item, but is not able to get their name?
like image 771
Nicolas Marigny Avatar asked Nov 08 '12 20:11

Nicolas Marigny


1 Answers

You're running up against a few breaking changes in the current version of ember-data.

The first is that, since revision 6 of ember-data, IDs are string-normalized and must be represented as strings in fixtures. Note that the REST adapter will convert numbers/strings, but the fixture adapter doesn't do any conversions. This is a common source of confusion (see the previous question).

The second is that support for embedded data objects has been temporarily removed from ember-data. I'm pretty sure that this feature will be re-introduced in a better way than supporting {embedded: true} in the attributes. IMO, embedding is more of an adapter concern and doesn't really belong with the definition of the model.

I adjusted your fixtures and got your example working here: http://jsfiddle.net/dgeb/zHz4Y/

like image 185
Dan Gebhardt Avatar answered Sep 23 '22 17:09

Dan Gebhardt