简体   繁体   English

Ember的单向约束输入?

[英]One-way bound inputs in Ember?

Ember's default {{input}} helper creates a two-way binding. Ember的默认{{input}}助手创建了双向绑定。 What's the state-of-the-art way to create an input element that is only one-way bound? 创建仅受单向限制的输入元素的最先进方法是什么?

A Google search for "ember one-way input" yields several add-ons: 谷歌搜索“ember单向输入”会产生几个附加组件:

but do I need an add-on? 但我需要一个附加组件吗?

The simplest way is to use the normal html <input> instead of the {{input}} helper: 最简单的方法是使用普通的html <input>而不是{{input}}帮助器:

<input value={{foo}} />

and if you want to trigger an action on change: 如果您想触发对更改的操作:

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

This is one-way bound, and works as expected. 这是单向绑定,并按预期工作。

I belive you can achieve this with the help of oneWay() helper. 我相信你可以在oneWay()助手的帮助下实现这一点。 Check the following 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 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-绑定

Sample code from ember guides, 来自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 works with two-way binding by default. input helpers默认使用双向绑定。

There are different alternatives to achieve one-way behaviour: 有不同的替代方法来实现单向行为:

  1. rendering your own html input tag: twiddle-1 渲染你自己的html input标签: twiddle-1
  2. creating a template-less component whose tagname is input : twiddle-2 创建一个标签名为input的无模板组件: twiddle-2
  3. using addons as you've mentioned. 正如你所提到的那样使用插件。

You do not need an addon for one-way bindings - https://www.emberjs.com/api/classes/Ember.computed.html#method_oneWay 您不需要用于单向绑定的插件 - https://www.emberjs.com/api/classes/Ember.computed.html#method_oneWay

But I'd say I have found https://github.com/DockYard/ember-one-way-controls quite helpful in day to day development. 但我会说我发现https://github.com/DockYard/ember-one-way-controls在日常开发中非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM