Ember无法保留下拉值

时间:2016-12-02 00:11:02

标签: ember.js ember-cli ember-select

我使用的是Ember 2.5.0。在我的应用程序中,我使用ember-select-list插件创建了一个带有下拉列表的页面。

我可以渲染下拉列表,但无法保留下拉列表的值。每当我选择值时,我在chrome控制台中都会遇到以下异常:

  

断言失败:无法在未定义的对象上使用'id'调用get

请查看以下代码,以供参考:

模板:

{{select-list content=roles 
              optionValuePath="role"
              optionLabelPath="role" 
              value=role 
              action=(action (mut role))}}

Route.js

export default Ember.Route.extend({
   model(){
      return Ember.RSVP.hash({
         books : this.store.findAll("book"),
         roles : this.store.findAll("role")
    });
},
setupController(controller,model){
   controller.set('users',model.books);
   controller.set('roles',model.roles);
 }
});

模型

export default DS.Model.extend({
    role:DS.attr()
});

在路由器中,当我通过Array(roles: this.store.findAll("role").toArray())而不是model时,我可以保留该值,但在传递模型时会抛出错误。

任何人,您能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

ember-select-list documentationunit test表示array帮助商的content媒体资源需要{{select-list}}

这就是Ember.RSVP.hash似乎失败的原因,因为hash期望并返回object,这是一种未配置使用ember-select-list的类型。

而不是hash,您应该使用Ember.RSVP.allall期望并返回array,这应该有效。

我已经创建了Ember Twiddle example来演示。

如果您不想使用ember-select-list,您可能会发现仅使用Ember's each helper构建自己的选择列表更容易,例如:

<select onchange={{action "selectRole" value="target.value"}}>
  {{#each roles as |role| }}
    <option value="{{ role }}" label={{ role }}>
      {{ role }}
    </option>
  {{/each}}
</select>

答案 1 :(得分:0)

我建议您使用ember-power-select插件。

对于你的问题,你可以试试这个,

setupController(controller,model){
   controller.set('users',model.books);
   controller.set('roles',model.roles.toArray());
 }

如果这不起作用,请告诉我