Ember.Select“value”不与绑定保持同步

时间:2014-02-15 14:50:22

标签: ember.js

我遇到了Ember.Select的问题,当“内容”绑定发生变化时,“值”绑定不会保持同步(返回第一个值)。

这是一个JSBin:http://emberjs.jsbin.com/kafod/1

基本上,更改类别会更改Ember.Select视图的可用值,并恢复为第一个可用值。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

Ember.Select组件需要选择一个值,因此当您切换类别时,sort字段会被覆盖。一个简单的解决方案是根据所选的类别,在模板中切换两个排序字段sortAccomodationssortEvents以及两个Ember.Selects

有关工作示例,请参阅http://emberjs.jsbin.com/kafod/3/

{{#if categoryIsAccommodations}}
    <br />Sort by {{view Ember.Select content=sortOptions
    optionValuePath="content.key"
    optionLabelPath="content.text"
    value=sortAccomodations}}
{{/if}}
{{#if categoryIsEvents}}
    <br />Sort by {{view Ember.Select content=sortOptions
    optionValuePath="content.key"
    optionLabelPath="content.text"
    value=sortEvents}}
{{/if}}

具有一个'sort'值的解决方案:

{{view Ember.Select content=sortOptions
   optionValuePath="content.key"
   optionLabelPath="content.text"
   value=sort}}

控制器:

  saveSorts: (->
    this.set('sortAccommodations', this.get('sort')) if this.get('category') is 'accommodations'
    this.set('sortEvents', this.get('sort')) if this.get('category') is 'events'
  ).observes('sort')

  categoryDidChange: (->
    this.set('sort', this.get('sortAccommodations')) if this.get('category') is 'accommodations'
    this.set('sort', this.get('sortEvents')) if this.get('category') is 'events'
  ).observes('category')

http://emberjs.jsbin.com/kafod/5/

相关问题