Ember.js EmberJS:如何在使用Fixture适配器时检索相关记录

Ember.js EmberJS:如何在使用Fixture适配器时检索相关记录,ember.js,Ember.js,我有两种型号: 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('fil

我有两种型号:

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')
下面是一个示例
neuron
fixture和一个示例
plot
fixture。我的实际申报有更多记录:

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
  }
]
我有一个嵌套的路径neuron/:neuron_id/plots,它应该将模型设置为特定神经元的plots:

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')
但是当我转到/neurons/:neuron\u id/plots时,没有加载任何绘图。没有错误,但我可以在Chrome Ember Inspector中看到,没有为路线绑定任何模型。我确信我正在加载的神经元记录的
plots
字段中的所有绘图都存在于
Plot.FIXTURES
声明中。我做错了什么


Nueron Plots路径无法获取神经元id,因此您可能正在查询未定义的。您可以使用modelFor获取上面路由的资源,然后在其上调用get plots,因此它看起来像这样(请原谅我缺乏coffeescript技能)


您可以使用needs在控制器中执行此操作-

谢谢,它成功了!似乎很奇怪,URL参数对子路由不可用。这是一个在过多人中争论的问题。部分原因是路由可以定义不同的url参数,他们希望避免参数冲突。有办法解决这个问题,但到目前为止还没有做到。
BuzsakiViewer.NeuronPlotsRoute = Ember.Route.extend
  model: (params) ->
    @modelFor('nueron').get('plots')