Получить принадлежность к ID без получения записи

Я пытаюсь получить идентификатор belongsTo без получения фактической записи. Мой JSON API возвращает идентификатор для отношения belongsTo.

У меня есть следующие модели

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()
)

Это мой маршрут:

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

Это мой контроллер:

Я пытаюсь найти идентификатор продукта внутри этого контроллера.

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
        )

Мой JSON выглядит следующим образом:

HTTP GET:/api/v1/recipes/1

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

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

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

Ответ 2

Тонная работа перестраивает отношения, вы можете вытащить ее из базовых свойств из атрибута data model.get('data.product.id')

Пример: http://emberjs.jsbin.com/OxIDiVU/16/edit

Ответ 3

Для ED 2.x отношения были переработаны и были нарушены для получения базовых данных до тех пор, пока функция ds-references не приземлилась.

Для этого в ED 2.4+ вам нужно использовать новый метод belongsTo для работы с базовыми данными.

Чтобы получить идентификатор для ссылки на ref:

var id = this.get('model').belongsTo('myBelongsToKey').value();