使用Ember-data检索从RecordArray中填充Ember.Select视图的内容

ian*_*zer 5 ember.js

我有一个使用此代码创建的选择列表:

      {{view Ember.Select
      contentBinding="App.riskCategories"
      optionValuePath="content.id"
      optionLabelPath="content.name"
      selectionBinding="riskCategory"
      class="input-medium"}}
Run Code Online (Sandbox Code Playgroud)

riskCategory是的属性App.Facility加载的模板和列表模式App.RiskCategory填充了这样的代码:

App.ready = function() {
  App.riskCategories = App.RiskCategory.all()
}
Run Code Online (Sandbox Code Playgroud)

这样可以正常工作并填充选择列表,但只能将已加载到浏览器中的Risk Categories子集.如果我从浏览器控制台调用App.RiskCategory.find()然后加载其余的并且选择列表更新但是我无法让代码工作为我这样做.

所以我尝试过:

App.ready = function() {
  App.riskCategories = App.RiskCategory.find()
}
Run Code Online (Sandbox Code Playgroud)

要么:

App.ready = function() {
  App.RiskCategory.find()
  App.riskCategories = App.RiskCategory.all()
}
Run Code Online (Sandbox Code Playgroud)

但这两个都会导致以下错误:

Uncaught Error: Attempted to handle event `loadedData` on <App.Facility:ember417:1> while in state rootState.loaded.updated.uncommitted. Called with undefined
Run Code Online (Sandbox Code Playgroud)

我很感激有关更好地填充选择列表的任何帮助或建议.应将这些App.RiskCategory对象视为存储在db中的常量的不可变集合.每个App.Facility对象都与这些App.RiskCategories之一相关联

谢谢!

ian*_*zer 1

我通过将每个App.Facility的渲染包装在 {{#if isLoaded}} 中解决了这个问题

所以代码看起来像这样:

{{#each client.facilities}}
  {{#if isLoaded}}
    {{view Ember.Select
      contentBinding="App.riskCategories"
      optionValuePath="content.id"
      optionLabelPath="content.name"
      selectionBinding="riskCategory"
      class="input-medium"}}
  {{/if}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)

看来设置App.RiskCategory的App.Facility对象尚未完成加载,因此正在设置 默认的App.RiskCategory ,然后当触发dataLoaded事件时,会引发异常,因为该对象已经被加载修改的。