【问题标题】:Ember 2.1 Input Field Overwriting Computed PropertyEmber 2.1 输入字段覆盖计算属性
【发布时间】:2016-03-07 04:12:06
【问题描述】:

我有一个侦听模型的计算属性,看起来像:

groups: function() {
    let g = this.get('model.items.groups').map(function(obj) { return obj.group; });
    return g.join(',');
  }.property('model.items.groups'),

在我的模板中,我有以下输入字段:

{{input value=groups type="text" class="form-control" placeholder="Testme"}}

我注意到在通过 UI 提供输入后,Ember 检查器中groups 的值变成了字符串,不再是计算属性。如何在 Ember 2.1 中避免这种情况并让它仅更新计算属性?

【问题讨论】:

  • 你说的更新计算属性是什么意思?

标签: javascript ember.js


【解决方案1】:

这是因为{{input}} 助手默认使用双向绑定。当您在输入字段中写入时,它将写入value 属性。

我一直在使用dockyard's one-way-input插件,它默认提供了一个单向绑定的输入组件。

{{one-way-input
  value=groups
  update=(action 'updateSomething')
}}

然后在您使用该组件的任何地方:

actions : {
  updateSomething(value) {
    //Do Something with the value and update model.items.groups?
  }
}

这样,值总是从 groups 计算属性中读取,并且操作会更新值的来源(而不是计算属性)

【讨论】:

    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多