Accessing rails relationships with Ember.js

风流意气都作罢 提交于 2019-12-24 19:37:03

问题


I'm just starting to learn ember and am writing a simple app that reads from a database. I've gotten it to work with fixtures how I want and have just started making some progress reading from a database. 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. However, I don't know where to go from here to get the parent class in ember to read and display the children elements. 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! 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 :)

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

  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

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

回答1:


Have you tried defining your embedded records boots in your router map?

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.



来源:https://stackoverflow.com/questions/17226175/accessing-rails-relationships-with-ember-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!