gpt4 book ai didi

ember.js - 获取belongsTo ID而不获取记录

转载 作者:行者123 更新时间:2023-12-02 16:39:51 25 4
gpt4 key购买 nike

我正在尝试获取belongsTo ID,而不获取实际记录。我的 JSON API 返回“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)
)

这是我的 Controller :

我正在尝试在此 Controller 中查找产品 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/ingredients?ids[]=2&ids[]=1

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

最佳答案

现在,通过添加到 ember 数据中的 ds-reference 功能,这变得更加容易。您现在可以执行以下操作:

var authorId = post.belongsTo("author").id();

参见http://emberjs.com/blog/2016/05/03/ember-data-2-5-released.html#toc_code-ds-references-code

关于ember.js - 获取belongsTo ID而不获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20479179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com