简体   繁体   English

绑定Ember v1.12组件中的HTML输入值

[英]Binding HTML Input value in Ember v1.12 Component

I need to use an HTML input in a component and need the value to be bound.The input helper causes various bugs when used on certain mobile devices, so that cannot be used. 我需要在组件中使用HTML输入并需要绑定值。输入助手在某些移动设备上使用时会导致各种错误,因此无法使用。

The input that works in Ember 1.13 or higher. 输入在Ember 1.13或更高版本中有效。

component/name-input.hbs

<input type="text" value={{name}} onChange={{action 'nameDidChange' value='target.value'}}>

component/name-input.js

export default Ember.Component.extend({
    name: 'Jim',
    actions: {
        nameDidChange: function(value) {
            this.set('name', value);
        }
    }
});

How do I use an HTML input in Ember 1.12 and still maintain the functionality? 如何在Ember 1.12中使用HTML输入并仍然保持功能?

I tried something like this but the input value is not passed to component's action: 我试过这样的东西,但输入值没有传递给组件的动作:

component/name-input.hbs

<input type="text" value={{name}} {{action 'nameDidChange' value='target.value' on='change'}}>

component/name-input.js

export default Ember.Component.extend({
    name: 'Dwight',
    actions: {
        nameDidChange: function(value) {
            this.set('name', value);
        }
    }
});

Here are two Twiddles, one in 1.13 that works as expected and one in 1.12 that does not work. 这里有两个Twiddles,一个在1.13中按预期工作,一个在1.12中不起作用。

Twiddle - 1.13 旋转 - 1.13

Twiddle - 1.12 旋转 - 1.12

As far as I know you cannot set value of action helper prior to Ember 1.13 (I would really be happy to be notified if I am wrong). 据我所知,你不能在Ember 1.13之前设置动作助手的值(如果我错了,我真的很乐意收到通知)。 As a result, your twiddle is not working for version 1.12 and I do not know a way to fix it. 因此,你的twiddle不适用于版本1.12,我不知道如何解决它。

What I suggest is kind of a workaround solution; 我建议的是一种解决方案; which I did not like very much. 我不太喜欢。 I am posting it as an answer because it might just be the case it is an appropriate solution for you. 我发布它作为答案,因为它可能就是适合你的解决方案。 Please see the twiddle I created via modifying yours. 请查看我通过修改你创建的旋转 I just used jquery to extract the value of the html input and set it to name2 field of name-input component. 我只是使用jquery来提取html输入的值并将其设置为name-input组件的name2字段。 Hope this works for you. 希望这对你有用。

In Ember 1.12.2, the event object is available in your action, which you can use to acquire the value for name-input: 在Ember 1.12.2中,事件对象在您的操作中可用,您可以使用它来获取name-input的值:

actions: {
  nameDidChange() {
    const value = event.target.value; // <--
    this.set('name', value);
  },
},

I've created an Ember Twiddle example to demonstrate. 我已经创建了一个Ember Twiddle示例来演示。

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

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