0

I'm getting following (simplified) output from a RESTful API:

{products: [{
    product: { 
        id: 1, name: "T-Shirt red"
    },
    images: [{
        id: 1, size: 'm', url: 'http://db.co/t-shirt-red_m.jpg'
    }, {
        id: 2, size: 'xl', url: 'http://db.co/t-shirt-red_xl.jpg'
    }]
}, {
    product: {
        id: 2, name: "T-Shirt blue"
    },
    images: [{
        id: 3, size: 'm', url: 'http://db.co/t-shirt-blue_m.jpg'
    }, {
        id: 4, size: 'xl', url: 'http://db.co/t-shirt-blue_xl.jpg'
    }]
}]}

Using Ember version 12, how should the declaration of the Product model looks like and how can I traverse the results? Haven't been able to find any example in that direction.

Following to access the data doesn't work (I just can't find the right syntax):

var products = App.Product.find();                  // seems to work
var prodNames = products.getEach('product.name');   // doesn't work
var secondProd = products.getObject(1).get('name'); // doesn't work

Thanks a lot in advance! claudio.

Claudio Bredfeldt
  • 1,423
  • 16
  • 27
  • FYI, I'm using Ember-Data – Claudio Bredfeldt Mar 12 '13 at 21:31
  • should those image arrays really be outside of the product hash? – Christopher Swasey Mar 12 '13 at 21:37
  • Also can you include your current App.Product declaration – Christopher Swasey Mar 12 '13 at 21:41
  • Yes, would be nice if I could keep Images outside. – Claudio Bredfeldt Mar 12 '13 at 21:44
  • `App.Product = DS.Model.extend({ 'product': { 'name': DS.attr('string') } });` I'm skipping Images for now (don't know how to do it). And I know, it isn't right like it is, but can't come up with something else. – Claudio Bredfeldt Mar 12 '13 at 21:47
  • Well, first, you can't nest things in your model defintion like that. But, you don't need to. The default REST adapter already expects a 'product' key at the root of the JSON for products. So, you could just do `App.Product = DS.Model.extend({ name: DS.attr('string') });` – Christopher Swasey Mar 12 '13 at 22:11
  • Yes, I had it like that at the beginning, but I still can't access my data (ie. products[0].product.name). How would you do it? – Claudio Bredfeldt Mar 12 '13 at 22:21
  • Technically, you can [declare a model property as array](http://stackoverflow.com/questions/12168570/how-to-represent-arrays-within-ember-data-models) using `DS.attr.transforms` – MilkyWayJoe Mar 12 '13 at 22:36
  • Thanks for the hint @MilkyWayJoe . Would be that the only way to use such a RESTful output as a model? It's not like I need to use `products[0].product.name` to access my data. For example something like this (well written) would be indeed better: `products.getEach('product.name')` – Claudio Bredfeldt Mar 12 '13 at 22:41

1 Answers1

1

DS.hasMany and some options for the REST adapter may help you.

I've used a similar setup with MongoDB embedded models. I've attached some examples below.

I didn't want to try to save to the embedded array, so I've used embedded:load, but you can use embedded: 'always' to persist the total object back to the server (although it didn't work quite as I expected) with 'always' if you save the parent object.

DS.RESTAdapter.map 'App.Check',
  line_items: { embedded: 'load' }
  parties: { embedded: 'load' }

App.Check = DS.Model.extend
  description: DS.attr("string")
  modified_date: DS.attr("date")
  parties: DS.hasMany('App.Party')

App.Party = DS.Model.extend
  name: DS.attr("string")
  check: DS.belongsTo('App.Party')

You can then reference the item. In a view, I've accessed it as below from a ArrayController where the content is set to an instance of DS.Check.

{{#each party in content.parties }}
nrion
  • 248
  • 1
  • 7