Gerçek kaydı almadan, belongsTo kimliğini almaya çalışıyorum. JSON API'mz, belongsTo ilişkisinin kimliğini döndürür.Kayıt alınmadan kayıt alın ID
Ben
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()
)
Bu benim rota aşağıdaki modeller olduğunu var: Ben bulmaya çalışıyorum
:
App.RecipeIndexRoute = Ember.Route.extend(
model: (args) ->
@store.find('recipe', args.recipe_id)
)
Bu
benim denetleyicisi olan Bu denetleyicideki ürün kimliği.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
)
Benim JSON şöyle görünür:
HTTP GET:/API/v1/tarifleri/1
{
"recipe": {
"id":1,
"title":"Recipe 1",
"ingredients":[2,1]
}
}
HTTP GET:/API/v1/maddeler kimlikleri [] = 2 & kimlikleri [] = 1
{
"ingredients": [
{
"id":2,
"recipe":1,
"product":1
},
{
"id":1,
"recipe":1,
"product":2
}
]
}
bakınız. Bu kodu kullandım: @ content.get ('maddeler') o zaman ((bileşenler) => ingredients.forEach ((bileşen, indeks, numaralandırılabilir) => productId = ingredient.toJSON(). Ürün # bu satır tetikleyicileri sunucuya çağrı – Martin
Bu ED içinde çirkin bir uygulama, bir PR yapmak ya da json için model getirme nedenini bulmak olabilir. – Kingpin2k
Bu gibi bir ürün id almak mümkün: ingredient.get ('data ') .product.id veya ingredient._data.product.id – Martin