Ember.js:Ember。选择valueBinding和延迟加载

时间:2012-12-30 18:51:23

标签: ember.js ember-data

我有一个带有禁用选择器的视图,一旦加载模型,就应该将其设置为来自ember-data的值。

LocationSelectView: Ember.Select.extend({
    prompt: "Choose location",
    contentBinding: 'controller.locations',
    optionValuePath: 'content.id',
    optionLabelPath: 'content.title',
    valueBinding: 'controller.content.location_id'
})

并在模板中禁用视图

{{view view.LocationSelectView disabled="true"}}

只要位置已经加载到商店,一切都按预期工作, 如果它们未加载,则内容按预期绑定(我可以通过启用选择器来验证),但所选值保持在“提示符”。

我通过在控制器的init中预加载位置数据来解决这个问题,但我真的不喜欢这个解决方案。

我该如何解决这个问题?这是一个错误吗?

2 个答案:

答案 0 :(得分:0)

此处提及此问题:https://github.com/emberjs/ember.js/issues/1333

我的解决方案是在视图中添加一个观察者:

preselect: function () {
  var item = this.get('items.firstObject');
  this.set('currentItem', item);
}.observes('items.@each')

假设使用selectionBinding(我更喜欢)而不是valueBinding

答案 1 :(得分:0)

我针对此问题开发了一种解决方法。请查看以下代码:

App.Select = Ember.Select.extend({
placeholder: '',
allowClear: false,

contentChanged: function() {
    if (this.get('value') === undefined && this.get('_iv') != undefined) {
        var v = this.get('_iv');
        var o = this.get('content').findProperty('id', v);

        if (o) {
            this.set('value', v);
        }
    }
}.observes('content.@each'),

init: function() {
    this._super();
    this.set('_iv', this.get('value'));
},

...

我希望它有所帮助

相关问题