2013-12-09 4 views
15

실제 레코드를 가져 오지 않고 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) 
) 

내 컨트롤러 이 컨트롤러 내의 제품 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/요리법/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 
    } 
    ] 
} 

답변

6

작품의 톤 재사용을 재실행으로가는 미리 구하고, 당신은 데이터 model.get('data.product.id')

예 속성에서 기본 속성 밖으로 뽑을 수 : ED 2.x를 들어 http://emberjs.jsbin.com/OxIDiVU/16/edit

+0

를 참조하십시오. 나는이 코드를 사용했다 : @ content.get ('ingredients'). then ((ingredients) => ingredients.forEach (성분, 색인, 열거 가능) => productId = ingredients.toJSON(). product # this line triggers 서버에 대한 호출 – Martin

+1

ED에서보기 싫은 구현입니다. PR을 수행해야하거나 json 용 모델을 가져 오는 이유를 찾아야 할 수도 있습니다. – Kingpin2k

+0

ingredient.get ('data ') .product.id 또는 ingredients._data.product.id – Martin

5

을, 관계를 재 작업되었으며, 기본 데이터 until the ds-references feature landed를 가져 오기위한 졌 있었다.

ED 2.4+에서 이렇게하려면 기본 데이터로 작업하려면 새로운 belongsTo 메서드를 사용해야합니다.

는 belongsTo를 심판의 ID를 얻으려면 :

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

ds-references가 방문했습니다. http://emberjs.com/blog/2016/05/03/ember-data-2-5-released.html#toc_code-ds-references-code – ianpetzer

관련 문제