1

I'm trying to figure out how to access a nested model in my fixture data. I can perfectly access the name, img_url etc. in an {{each}} loop, but i get [Object, object] if i try to do {{address.street}}. The model below:

App.Test = DS.Model.extend({
  name: attr(),
  img_url: attr(),
  description: attr(),
  address: {
    street: attr(),
    number: attr(),
    zip_code: attr(),
    city: attr()
  }
});
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
Jan_dh
  • 771
  • 6
  • 14
  • Have you tried logging to console these objects, so you know what properties does it contain? – Daniel Kmak Nov 02 '14 at 21:46
  • I did. This is what i got back: 'Ember Inspector ($E): Object {id: "3", name: "Test", img_url: "url", description: "This is a test", address: Object}' – Jan_dh Nov 02 '14 at 21:50
  • Try to console.log address property, so you can see how to access child properties. Then if there are more objects try to log these objects also. Maybe you will find property address.street.value etc. If you could set up a demo here - http://emberjs.jsbin.com/ – Daniel Kmak Nov 02 '14 at 21:53

1 Answers1

1

You should create a separate model type Address and create a relationship between the two models.

App.Test = DS.Model.extend({
  name: attr(),
  img_url: attr(),
  description: attr(),
  address: belongsTo('address', {async:true})
});

App.Address = DS.Model.extend({
  street: attr(),
  number: attr(),
  zip_code: attr(),
  city: attr()
});

Example: http://emberjs.jsbin.com/vuhaga/2/edit

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • I thought about doing that, but as the model is always linked, I don't really see why it should be two seperate models. In this Ember tutorial video he also uses author.name https://www.youtube.com/watch?v=1QHrlFlaXdI - can't seem to find anything official in the documentation on this on the ember website. – Jan_dh Nov 02 '14 at 22:04
  • He isn't using Ember Data, Ember and Ember Data are two different products. Ember works perfectly fine without Ember Data. http://stackoverflow.com/questions/24408892/ember-without-ember-data/24411550#24411550 , Even with this relationship setup you can still do `test.address.street` – Kingpin2k Nov 03 '14 at 02:07
  • I included an example above – Kingpin2k Nov 03 '14 at 02:20