20

I'm trying to fetch the belongsTo ID without fetching the actual record. My JSON API returns the ID for the belongsTo relation.

I have the following models

App.Recipe = DS.Model.extend(
  title: DS.attr()
  ingredients: DS.hasMany('ingredient', async: true)
)

App.Ingredient = DS.Model.extend(
  recipe: DS.belongsTo('recipe')
  product: DS.belongsTo('product', async: true)
)

App.Product = DS.Model.extend(
  title: DS.attr()
)

This is my route:

App.RecipeIndexRoute = Ember.Route.extend(
  model: (args) ->
    @store.find('recipe', args.recipe_id)
)

This is my controller:

I'm trying to find the product id within this controller.

App.RecipeIndexController = Ember.ObjectController.extend(
  hasRecipeInCart: null

  actions:
    toggleCart: ->
      @content.get('ingredients').then((ingredients) =>
        # how do I get product id here, without lookup up the product relation?
        # this 'get' makes Ember call: /api/v1/products/1
        # I simply want the product ID (in this case 1), without having to call the server again.
        ingredient.get('product').then((product) =>
          product.id # => 1
        )

My JSON looks like this:

HTTP GET: /api/v1/recipes/1

{
  "recipe": {
    "id":1,
    "title":"Recipe 1",
    "ingredients":[2,1]
  }
}

HTTP GET: /api/v1/ingredients?ids[]=2&ids[]=1

{
  "ingredients": [
    {
      "id":2,
      "recipe":1,
      "product":1
    },
    {
      "id":1,
      "recipe":1,
      "product":2
    }
  ]
}
Martin
  • 2,192
  • 2
  • 28
  • 42

3 Answers3

13

This is now much easier with the ds-reference feature that was added to ember data. You can now do:

var authorId = post.belongsTo("author").id();

See http://emberjs.com/blog/2016/05/03/ember-data-2-5-released.html#toc_code-ds-references-code

ianpetzer
  • 1,808
  • 1
  • 16
  • 20
  • If you want to get the id in a template, you need to write your own helper: [Ember Data issue #4407](https://github.com/emberjs/data/issues/4407) – Jacob van Lingen Oct 18 '18 at 13:27
6

A ton of work is going into redoing the relationships, you can pull it out of the underlying properties from the data attribute model.get('data.product.id')

Example: http://emberjs.jsbin.com/OxIDiVU/16/edit

Kingpin2k
  • 47,007
  • 10
  • 75
  • 96
  • That also triggers a call to the server: /api/v1/products/1, for some reason. I used this code: @content.get('ingredients').then((ingredients) => ingredients.forEach((ingredient, index, enumerable) => productId = ingredient.toJSON().product # this line triggers the call to the server. – Martin Dec 10 '13 at 08:15
  • 1
    That's a ugly implementation in ED, i might need to do a PR or find out why it's fetching the models for json. – Kingpin2k Dec 10 '13 at 18:00
  • It's possible to get the product id like this: ingredient.get('data').product.id or ingredient._data.product.id – Martin Dec 11 '13 at 00:24
  • 1
    This nolonger works in Ember 2. How do I get belongsTo id without fetching record in newer versions of Ember (and Ember data)? – Martin Dec 01 '15 at 08:29
  • @Kingpin2k did you ever open anything against Ed about this? – Adam Knights Feb 15 '16 at 10:04
  • 1
    There is an easier solution nowadays: http://emberjs.com/blog/2016/05/03/ember-data-2-5-released.html#toc_code-ds-references-code You can do something like: var authorId = post.belongsTo("author").id(); – ianpetzer Jul 19 '16 at 16:38
5

For ED 2.x, relationships have been reworked and were broken for getting underlying data until the ds-references feature landed.

To do this in ED 2.4+, you need to use the new belongsTo method to work with underlying data.

To get an id for a belongsTo ref:

var id = this.get('model').belongsTo('myBelongsToKey').value();
James_1x0
  • 892
  • 10
  • 20
  • 1
    ds-references has landed :) http://emberjs.com/blog/2016/05/03/ember-data-2-5-released.html#toc_code-ds-references-code – ianpetzer Jul 19 '16 at 16:37