실제 레코드를 가져 오지 않고 belongsTo ID를 가져 오려고합니다. 내 JSON API는 belongsTo 관계에 대한 ID를 반환합니다.레코드를 가져 오지 않고 belongsTo ID 가져 오기
나는
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)
)
이
내 컨트롤러 이 컨트롤러 내의 제품 IDApp.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/요리법/1
{
"recipe": {
"id":1,
"title":"Recipe 1",
"ingredients":[2,1]
}
}
HTTP GET :/API/V1/재료 IDS [] 2 개 = & ID는 [] = 1
{
"ingredients": [
{
"id":2,
"recipe":1,
"product":1
},
{
"id":1,
"recipe":1,
"product":2
}
]
}
를 참조하십시오. 나는이 코드를 사용했다 : @ content.get ('ingredients'). then ((ingredients) => ingredients.forEach (성분, 색인, 열거 가능) => productId = ingredients.toJSON(). product # this line triggers 서버에 대한 호출 – Martin
ED에서보기 싫은 구현입니다. PR을 수행해야하거나 json 용 모델을 가져 오는 이유를 찾아야 할 수도 있습니다. – Kingpin2k
ingredient.get ('data ') .product.id 또는 ingredients._data.product.id – Martin