Javascript 使用Ember.js访问rails关系
我刚刚开始学习余烬,正在编写一个从数据库读取的简单应用程序。我已经按照自己的意愿使用了fixture,并且刚刚开始从数据库中读取数据,取得了一些进展。现在我的问题是我不能访问子元素——我有一个父类,我通过序列化程序用json响应它,我在json请求中为子元素提供服务。但是,我不知道从这里到哪里才能让ember中的父类读取和显示子元素。使用下面的代码可能更有意义 让我知道,如果你需要任何其他代码-它的所有标准烬代码没有规格!我唯一的线索是我当前的嵌套ruby路由充当队列/:id/boots/:id,而余烬在使用fixture数据时加载队列/:id/:id:) 型号:Javascript 使用Ember.js访问rails关系,javascript,ruby-on-rails,ruby,ember.js,ember-data,Javascript,Ruby On Rails,Ruby,Ember.js,Ember Data,我刚刚开始学习余烬,正在编写一个从数据库读取的简单应用程序。我已经按照自己的意愿使用了fixture,并且刚刚开始从数据库中读取数据,取得了一些进展。现在我的问题是我不能访问子元素——我有一个父类,我通过序列化程序用json响应它,我在json请求中为子元素提供服务。但是,我不知道从这里到哪里才能让ember中的父类读取和显示子元素。使用下面的代码可能更有意义 让我知道,如果你需要任何其他代码-它的所有标准烬代码没有规格!我唯一的线索是我当前的嵌套ruby路由充当队列/:id/boots/: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
您是否尝试过在路由器映射中定义嵌入的记录
boots
例如:
Plato.Adapter = DS.RESTAdapter.extend();
Plato.Store = DS.Store.extend({
adapter: Plato.Adapter
});
Plato.Adapter.map('Plato.Cohort', {
boots: {embedded: 'always'}
});
这样,嵌入的记录将与父记录一起加载
希望能有帮助。我很高兴它能工作,为什么
{embedded:'always'}
:)也许另一个问题的答案会帮助我理解-我想让嵌套深入到两个层次,但我似乎无法重新创建或嵌套嵌入式函数,使其工作于最低层次(第三层?)嵌套属性层-虽然我似乎找不到关于嵌入式的太多信息,但我会继续查找!我想这是因为我对RestaAdapter缺乏了解
Plato.Adapter = DS.RESTAdapter.extend();
Plato.Store = DS.Store.extend({
adapter: Plato.Adapter
});
Plato.Adapter.map('Plato.Cohort', {
boots: {embedded: 'always'}
});