Sean Mackesey
Sean Mackesey

Reputation: 10959

EmberJS: How to retrieve associated records when using the Fixture adapter

I have two models:

BuzsakiViewer.Neuron = DS.Model.extend
  number: DS.attr()
  plots: DS.hasMany 'plot', { async: true }

BuzsakiViewer.Plot = DS.Model.extend
  filename: DS.attr()
  neuron: DS.belongsTo('neuron')
  path: ( ->
    'images' + '/' + @get('filename') 
  ).property('filename')

Below is a sample neuron fixture and a sample plot fixture. My actual declarations have more records:

BuzsakiViewer.Neuron.FIXTURES = [
  {
    "id": 1,
    "plots": [ 80, 81, 82, 83, 84, 85, 86, 87 ]
  }
]

BuzsakiViewer.Plot.FIXTURES = [
  {
    "filename": "n1_avgleft.png",
    "id": 80,
    "neuron": 1
  }
]

I have a nested route neuron/:neuron_id/plots, which should set the model to the plots for a particular neuron:

BuzsakiViewer.Router.map ->
  @resource 'neurons'
  @resource 'neuron', path: '/neurons/:neuron_id', ->
    @route 'plots'
  @resource 'plots'
  @resource 'plot', path: '/plots/:plot_id'

BuzsakiViewer.NeuronPlotsRoute = Ember.Route.extend
  model: (params) ->
    @store.find('neuron', params.neuron_id).then (neuron) ->
      neuron.get('plots')

But when I go to /neurons/:neuron_id/plots, no plots are loaded. There is no error, but I can see in the Chrome Ember Inspector that no model is bound for the NeuronPlots route. I am sure that all the plots in the plots field of the neuron record I'm loading are present in the Plot.FIXTURES declaration. What am I doing wrong?

enter image description here

Upvotes: 0

Views: 117

Answers (2)

Nath
Nath

Reputation: 6864

You could do this in a controller using needs - Dependencies between controllers

Upvotes: 0

Kingpin2k
Kingpin2k

Reputation: 47367

The Nueron Plots route won't get the neuron_id, so you are probably querying for undefined. You can use modelFor to get the resource of the route above and then call get plots on it, so it will look something like this (pardon my lack of coffeescript skills)

BuzsakiViewer.NeuronPlotsRoute = Ember.Route.extend
  model: (params) ->
    @modelFor('nueron').get('plots')

Upvotes: 1

Related Questions