Sto cercando di recuperare l'ID di proprietà senza recuperare il record effettivo. La mia API JSON restituisce l'ID per la relazione belongsTo.Ottieni ID proprietà senza recupero record
ho i seguenti modelli
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()
)
Questo è il mio percorso:
App.RecipeIndexRoute = Ember.Route.extend(
model: (args) ->
@store.find('recipe', args.recipe_id)
)
Questo è il mio controllo:
Sto cercando di trovare la ID prodotto all'interno di questo 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
)
mio JSON assomiglia a questo:
HTTP GET:/api/v1/Ricette/1
{
"recipe": {
"id":1,
"title":"Recipe 1",
"ingredients":[2,1]
}
}
HTTP GET:?/API/v1/Ingredienti ids [] = 2 & ids [] = 1
{
"ingredients": [
{
"id":2,
"recipe":1,
"product":1
},
{
"id":1,
"recipe":1,
"product":2
}
]
}
che innesca anche una chiamata al server:/api/v1/prodotti/1, per qualche ragione. Ho usato questo codice: @ content.get ('ingredients'). Then ((ingredients) => ingredients.forEach ((ingrediente, indice, enumerabile) => productId = ingredient.toJSON(). Product # questa riga trigger la chiamata al server – Martin
Questa è una brutta implementazione in ED, potrei aver bisogno di fare un PR o scoprire perché sta recuperando i modelli per JSON – Kingpin2k
È possibile ottenere l'id del prodotto in questo modo: ingredient.get ('data ') .product.id o ingredient._data.product.id – Martin