[英]Accessing rails relationships with Ember.js
我剛剛開始學習ember,正在編寫一個從數據庫讀取的簡單應用程序。 我已經將它與我想使用的fixtures一起使用,並且剛剛開始從數據庫讀取中取得一些進展。 現在,我的問題是我無法訪問子元素-我有一個父類,我通過序列化器用json響應,並且正在json請求中提供子元素。 但是,我不知道從這里到哪里去使ember中的父類讀取和顯示child元素。 使用下面的代碼可能更有意義。
讓我知道您是否需要任何其他代碼-這是標准的Ember代碼,無規范! 我唯一想得到的線索是我當前的嵌套紅寶石路線用作隊列/:id /靴子/:id,而在使用夾具數據加載隊列時使用余燼隊列:: id /:id :)
楷模:
Plato.Boot = DS.Model.extend(
name: DS.attr("string"),
cohort: DS.belongsTo('Plato.Cohort'),
hubs: DS.hasMany('Plato.Hub')
)
Plato.Cohort = DS.Model.extend(
name: DS.attr('string'),
boots: DS.hasMany('Plato.Boot')
)
Route.rb
root to: 'application#index'
resources :cohorts do
resources :boots
end
resources :boots
同類群組(父級)控制器
class CohortsController < ApplicationController
respond_to :json
def index
respond_with Cohort.all
end
def show
respond_with Cohort.find(params[:id])
end
end
引導(子)控制器
class BootsController < ApplicationController
respond_to :json
def index
respond_with Boot.all
end
def show
respond_with Boot.find(params[:id])
end
end
Router.js.coffee
Plato.Router.map ()->
this.resource('cohorts', ->
this.resource('cohort', {path: '/:cohort_id'}, ->
this.resource('boot', {path: 'boots/:boot_id'})
)
)
Plato.CohortsRoute = Ember.Route.extend(
model: ->
Plato.Cohort.find()
)
Plato.BootsRoute = Ember.Route.extend(
model: ->
Plato.Boot.find(params)
)
您是否嘗試過在路由器映射中定義嵌入式記錄boots
?
例如:
Plato.Adapter = DS.RESTAdapter.extend();
Plato.Store = DS.Store.extend({
adapter: Plato.Adapter
});
Plato.Adapter.map('Plato.Cohort', {
boots: {embedded: 'always'}
});
這樣,嵌入式記錄將與父記錄一起加載。
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.