Ember.js textField change event

安稳与你 提交于 2019-12-12 21:23:50

问题


I just start to learn ember.js Why this code http://jsfiddle.net/alexchetv/RVNzP/ don't work properly -- App.MyTextField.change() execution is triggered only after MyTextField loses focus? Alternative code with the same functionality works as expected.


回答1:


Keep in mind that handlers on your Ember.Views (change, select, click, etc) are bound to normal HTML DOM events. "onchange" only gets called on an input field when the field loses focus and has changed, not any time the value is modified. You should observe 'value' if you want to be notified of changes to the displayed value.




回答2:


Here's a working solution.

What I've done is made formDirty a computed value, which recomputes upon changes in the input. Ember, unlike the native "change" event, updates the the Input's value on each keystroke (copy/paste event etc).
Ember's binding makes sure the peopleController is always updated with the input value, thus also updating the computed field formDirty.

Note that if you add more inputs, you must tell the computed property to listen to its events, e.g.

formDirty: function() {
    return !Ember.empty(this.get('fName')) && !Ember.empty(this.get('lName'));;
}.property('lName', 'fName').cacheable() // <- dependencies   

cacheable() is used for performance sake only, meaning don't computed again until the dependencies change.



来源:https://stackoverflow.com/questions/10538439/ember-js-textfield-change-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!