Christoffer
Christoffer

Reputation: 514

hasMany relation in Ember data finds no relations

I'm using Ember Data 0.14.

I have this data:

{
  "product": {
    "id": 185588,
    "name": "USB MIDI Adapter"
  },
  "images": [
    {
      "url": "google.com",
      "product_id": 185588
    },
    {
      "url": "google.com2",
      "product_id": 185588
    }
  ]
}

And these models:

App.Image = DS.Model.extend({
    url: DS.attr('string'),
    product_id: DS.belongsTo('App.Product')
});

App.Product = DS.Model.extend({
    name: DS.attr('string'),
    images: DS.hasMany('App.Image') 
});

DS.RESTAdapter.map('App.Product', {
  images: { embedded: 'always' }
});

But I cannot get the relation to work, the "images" on product is empty. Isn't this how it's supposed to work?

Upvotes: 0

Views: 118

Answers (2)

Marcio Junior
Marcio Junior

Reputation: 19128

You need to embed the data inside of images array:

{
  "product": {
    "id": 185588,
    "name": "USB MIDI Adapter",
    "images": [{
      "id": 1,
      "url": "google.com",
      "product_id": 185588
    },
    {
      "id": 2,
      "url": "google.com2",
      "product_id": 185588
    }]
  }          
}

http://jsfiddle.net/marciojunior/TEgK7/ (Using Ember Data 0.14)

Upvotes: 2

Kingpin2k
Kingpin2k

Reputation: 47367

you're missing the mappings from the product, embedded was disabled due to issues for the time being. https://github.com/emberjs/data/blob/master/TRANSITION.md

{
  "product": {
    "id": 185588,
    "name": "USB MIDI Adapter",
    "images":["imageid1", "imageid2"....]
  },
  "images": [
    {
      "id": "imageid1",
      "url": "google.com",
      "product_id": 185588
    },
    {
      "id": "imageid2",
      "url": "google.com2",
      "product_id": 185588
    }
  ]
}

http://emberjs.jsbin.com/OxIDiVU/40/edit

Upvotes: 1

Related Questions