Ember 中的单向绑定输入?

One-way bound inputs in Ember?

Ember 的默认 {{input}} 助手创建双向绑定。创建仅单向绑定的输入元素的最先进方法是什么?

Google 搜索 "ember one-way input" 会产生几个附加组件:

但是我需要一个附加组件吗?

我相信你可以在 oneWay() 助手的帮助下实现这一目标。检查以下 link.

https://www.emberjs.com/api/classes/Ember.Binding.html#toc_one-way-bindings https://guides.emberjs.com/v2.13.0/object-model/bindings/#toc_one-way-bindings

来自 ember 指南的示例代码,

user = Ember.Object.create({
  fullName: 'Kara Gates'
});

UserComponent = Ember.Component.extend({
  userName: Ember.computed.oneWay('user.fullName')
});

userComponent = UserComponent.create({
  user: user
});

// Changing the name of the user object changes
// the value on the view.
user.set('fullName', 'Krang Gatessss');
// userComponent.userName will become "Krang Gatessss"

// ...but changes to the view don't make it back to
// the object.
userComponent.set('userName', 'Truckasaurus Gates');
user.get('fullName'); // "Krang Gatessss"

input helpers 默认使用双向绑定。

实现单向行为有不同的选择:

  1. 呈现您自己的 html input 标签:twiddle-1
  2. 正在创建标记名为 input 的无模板组件:twiddle-2
  3. 使用您提到的插件。

最简单的方法是使用普通的 html <input> 而不是 {{input}} 助手:

<input value={{foo}} />

如果您想触发更改操作:

<input value={{foo}} onchange={{action 'changeFoo' value="target.value"}} />

这是单向绑定,并且按预期工作。

单向绑定不需要插件 - https://www.emberjs.com/api/classes/Ember.computed.html#method_oneWay

但我会说我发现 https://github.com/DockYard/ember-one-way-controls 在日常开发中很有帮助。