Holen Sie sich Gehört zu ID, ohne Datensatz abzurufen
Ich versuche, die zugehörige ID abzurufen, ohne den tatsächlichen Datensatz abzurufen. Meine JSON-API gibt die ID für die Gehört zu-Beziehung zurück.
Ich habe folgende Modelle
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()
)
Das ist meine Route:
App.RecipeIndexRoute = Ember.Route.extend(
model: (args) ->
@store.find('recipe', args.recipe_id)
)
Das ist mein Controller:
Ich versuche, die Produkt-ID in diesem Controller zu finden.
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
)
Mein JSON sieht so aus:
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
}
]
}