简体   繁体   English

使用Ember.js访问Rails关系

[英]Accessing rails relationships with Ember.js

I'm just starting to learn ember and am writing a simple app that reads from a database. 我刚刚开始学习ember,正在编写一个从数据库读取的简单应用程序。 I've gotten it to work with fixtures how I want and have just started making some progress reading from a database. 我已经将它与我想使用的fixtures一起使用,并且刚刚开始从数据库读取中取得一些进展。 Right now my problem is that I can't access children elements - I have a parent class that I am responding to with json through a serializer, and I am serving the children elements in the json request. 现在,我的问题是我无法访问子元素-我有一个父类,我通过序列化器用json响应,并且正在json请求中提供子元素。 However, I don't know where to go from here to get the parent class in ember to read and display the children elements. 但是,我不知道从这里到哪里去使ember中的父类读取和显示child元素。 It may make some more sense with the code below. 使用下面的代码可能更有意义。

Let me know if you need any other code - it's all the standard Ember code no specifications! 让我知道您是否需要任何其他代码-这是标准的Ember代码,无规范! The only lead I'm goin on is my current nested ruby routes are serving as cohorts/:id/boots/:id while ember when using fixture data loads cohorts/:id/:id :) 我唯一想得到的线索是我当前的嵌套红宝石路线用作队列/:id /靴子/:id,而在使用夹具数据加载队列时使用余烬队列:: id /:id :)

Models: 楷模:

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 Route.rb

  root to: 'application#index'

  resources :cohorts do
    resources :boots
  end

  resources :boots

Cohort (parent) controller 同类群组(父级)控制器

class CohortsController < ApplicationController
    respond_to :json
  def index
    respond_with Cohort.all
  end

  def show
    respond_with Cohort.find(params[:id])
  end
end

Boot (child) controller 引导(子)控制器

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

Have you tried defining your embedded records boots in your router map? 您是否尝试过在路由器映射中定义嵌入式记录boots

For example: 例如:

Plato.Adapter = DS.RESTAdapter.extend();

Plato.Store = DS.Store.extend({
  adapter: Plato.Adapter
});

Plato.Adapter.map('Plato.Cohort', {
  boots: {embedded: 'always'}
});

This way the embedded records will be loaded together with the parent records. 这样,嵌入式记录将与父记录一起加载。

Hope it helps. 希望能帮助到你。

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

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