简体   繁体   English

获取belongsTo ID而不获取记录

[英]Get belongsTo ID without fetching record

I'm trying to fetch the belongsTo ID without fetching the actual record. 我正在尝试获取belongsTo ID而不获取实际记录。 My JSON API returns the ID for the belongsTo relation. 我的JSON API返回belongsTo关系的ID。

I have the following models 我有以下型号

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()
)

This is my route: 这是我的路线:

App.RecipeIndexRoute = Ember.Route.extend(
  model: (args) ->
    @store.find('recipe', args.recipe_id)
)

This is my controller: 这是我的控制器:

I'm trying to find the product id within this 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
        )

My JSON looks like this: 我的JSON看起来像这样:

HTTP GET: /api/v1/recipes/1 HTTP GET:/ api / v1 / recipes / 1

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

HTTP GET: /api/v1/ingredients?ids[]=2&ids[]=1 HTTP GET:/ api / v1 / ingredients?ids [] = 2&ids [] = 1

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

This is now much easier with the ds-reference feature that was added to ember data. 现在,使用添加到ember数据的ds-reference功能可以更轻松。 You can now do: 你现在可以这样做:

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

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

A ton of work is going into redoing the relationships, you can pull it out of the underlying properties from the data attribute model.get('data.product.id') 大量的工作是重做关系,你可以从数据属性model.get('data.product.id')它从基础属性中拉出来

Example: http://emberjs.jsbin.com/OxIDiVU/16/edit 示例: http//emberjs.jsbin.com/OxIDiVU/16/edit

For ED 2.x, relationships have been reworked and were broken for getting underlying data until the ds-references feature landed . 对于ED 2.x,关系已经过重新设计,并且在获取基础数据之前已被破坏, 直到ds-references功能着陆为止

To do this in ED 2.4+, you need to use the new belongsTo method to work with underlying data. 要在ED belongsTo执行此操作,您需要使用新的belongsTo方法来处理基础数据。

To get an id for a belongsTo ref: 要获取belongsTo ref的id:

var id = this.get('model').belongsTo('myBelongsToKey').value();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM