Just starting out with Ember, I wanted to perform some action when a user types something in a text field. I first tried this
<input type="text" {{bind-attr value=qty}} {{action "checkQty" on="keyUp"}}/>
That correctly initialized the text field's value and called the checkQty()
function in my controller's actions
hash, but I was not getting the updated value for qty
. So it seems bind-attr
is a one-way deal.
I tried using the TextField
view:
{{view Ember.TextField value=qty onEvent="keyPress" action="checkQty"}}
I had to use keyPress
instead of keyUp
because onEvent only allows enter or keyPress. This worked, except that reading qty's value on keyPress
gives me the value of the field before the user pressed the key and not afterward.
So I wound up using the input helper to specify the field:
{{input type="text" value=qty}}
And created an observer in my controller:
checkQty: function() {
Ember.Logger.log('qty: ', this.get('qty'));
}.observes('qty').on('change')
So that works, but now I am confused about best practices for architecting my Ember app. It seems like the prescribed method is for your template to effectively call actions on your controller. My usage of an observer feels like a hack to get around my failure to get this working in a template.
After looking at the Ember source code, I was able to extend TextField
to get the behavior I wanted:
MyApp.TextField = Ember.TextField.extend({
keyUp: function(event) {
this.sendAction('action', this.value);
}
});
And my template then became:
{{view MyApp.TextField value=qty onEvent="keyUp" action="checkQty"}}
Is there a better way to accomplish this in Ember?