에있는 사용자 정의 시리얼 라이저에서만 인덱스 경로의 데이터를 사이드로드하지 마십시오. Ember에서 /recipes
인덱스 경로를 찾으려하고 있는데, 특정 뷰에 불필요한 추가 JSON 데이터를 사이드로드하지 않도록 선택할 수 있습니다. 개별 레시피 경로에 해당 데이터를 사이드로드합니다.ActiveModel :: Serializers
는 레일은 JSON 페이로드를 반환, 지금 일하고 방법은 내가 원하는 :
/recipes.json
{
"recipes": [
{
"id": 1,
"title": "Scrambled Eggs"
}
]
}
/recipes/1.json
{
"steps": [
{
"id": 1,
"substep_ids": [
1,
2
]
}
],
"substeps": [
{
"id": 1
},
{
"id": 2
}
],
"recipe": {
"id": 1,
"title": "Scrambled Eggs",
"step_ids": [
1
]
}
}
내 엠버 응용 프로그램에 /recipes
로 이동하는 경우 , 렌더링됩니다.
- 스크램블 계란
- 해시 브라운
하지만이 link-to
클릭하면 다음 내가 /recipes/:recipe_id
API를 요청할 때 사이드로드 될 적절한 단계와 하위 단계를 볼 수 없습니다. 해당 페이지를 수동으로 새로 고치면 데이터가 성공적으로 사이드로드됩니다.
내가 원하는 것을 할 수있는 방법이 있습니까? 레일 측면에서
:
class RecipesController < ApplicationController
def index
@recipes = Recipe.all.includes(steps: [:substeps])
render json: @recipes, each_serializer: ShortRecipeSerializer
end
def show
@recipe = Recipe.includes(:steps => [:substeps]).find(params[:id])
render json: @recipe
end
end
class RecipeSerializer < ActiveModel::Serializer
embed :ids, include: true
has_many :steps
attributes :id, :title
end
class ShortRecipeSerializer < ActiveModel::Serializer
attributes :id, :title
end
그리고 엠버 측 :
Recipe = DS.Model.extend
title: DS.attr('string')
steps: DS.hasMany('step', {async: true})
Step = DS.Model.extend
substeps: DS.hasMany('substep', {async: true})
recipe: DS.belongsTo('recipe')
Substep = DS.Model.extend
step: DS.belongsTo('step')
미리 감사드립니다! 나는이 일을 생각
우수 답변. 내가 바꿀 유일한 것은'return this.store.find ('recipe', params.recipe_id)'이며, 제게는'ember-cli '로'RecipeRoute'가 될 것입니다. –