gpt4 book ai didi

ember.js - 观察输入组件的值

转载 作者:行者123 更新时间:2023-12-02 06:08:08 26 4
gpt4 key购买 nike

我有一个简单的组件,它是 input通过 tagName 的元素.

到目前为止没有问题。现在我想观察输入的值属性。每当输入改变时,我都想解雇一个观察者。

为什么这不起作用?

可以找到一个 JSFiddle here .

<script type="text/x-handlebars">
{{my-input}}
</script>

<script type="text/x-handlebars" id="components/my-input">
</script>

var App = Ember.Application.create();

App.MyInputComponent = Ember.Component.extend({
tagName: 'input',
attributeBinding: ['value'],
onChange: Ember.observer('value', function() {
console.log('change');
})
});

最佳答案

免责声明:此解决方案适用于 OP 使用的 ember 版本(ember-1.0.0-rc.6.js),在 future 的版本中,概念保持不变,只是语法有所改变。

(版本 1.13.6 https://emberjs.jsbin.com/xuhavokubi/edit?html,js,output 的示例,代码发布在最后)

原始代码的问题如下,

1.属性绑定(bind)属性为attributeBindings
2.Ember.observer在 future 的版本中工作得很好,但似乎在这个组件中不起作用(ember-1.0.0-rc.6.js)

3.添加了使用observes的替代观察者功能

4.增加处理dom值变化和通知ember属性的功能

示例,

对于版本 ember-1.0.0-rc.6.js (http://jsfiddle.net/3vre965u/)

hbs

<script type="text/x-handlebars">
{{my-input}}
</script>

<script type="text/x-handlebars" id="components/my-input">
the value:{{value}}<br/><button {{action "changeTheValue"}}>change value</button>
<button {{action "displayTheValues"}}>display values</button>
</script>

js
var App = Ember.Application.create();

App.MyInputComponent = Ember.Component.extend({
tagName: 'input',
attributeBindings: ['value'],
changeTheValue:function(){
this.set('value',Date.now());
},
displayTheValues:function(){
alert("ember value:\n"+this.get("value")+"\nDOM value:\n"+this.$().val());
},
//catches dom event
//fires only when focused lost
change:function(event){
this.set("value",event.target.value);
},
//catches dom event
keyUp:function(event){
this.set("value",event.target.value);
},
//works like this in future versions, does not work in current version
onChange: Ember.observer('value', function() {
console.log('change');
}),
//alternative observer that works fine in current version
onChange2: function(){
console.log('change2');
}.observes('value')
});

对于版本 1.13.6 ( https://emberjs.jsbin.com/xuhavokubi/edit?html,js,output)

hbs
<script type="text/x-handlebars">
{{my-input viewName="test"}}
<br/>
the value:{{view.test.value}}<br/><button {{action "changeTheValue" target="view.test"}}>change value</button>
<button {{action "displayTheValues" target="view.test"}}>display values</button>
</script>

<script type="text/x-handlebars" id="components/my-input">
</script>

js
var App = Ember.Application.create();

App.MyInputComponent = Ember.Component.extend({
tagName: 'input',
attributeBindings: ['value'],
actions:{
changeTheValue:function(){
this.set('value',Date.now());
},
displayTheValues:function(){
alert("ember value:\n"+this.get("value")+"\nDOM value:\n"+this.$().val());
}
},
//catches dom event
//fires only when focused lost
change:function(event){
this.set("value",event.target.value);
},
//catches dom event
keyUp:function(event){
this.set("value",event.target.value);
},
onChange: Ember.observer('value', function() {
console.log('change');
}),
//alternative observer
onChange2: function(){
console.log('change2');
}.observes('value')
});

关于ember.js - 观察输入组件的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31780577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com