简体   繁体   中英

Accessing rails relationships with Ember.js

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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