action on enter in ember

David picture David · Sep 7, 2013 · Viewed 21.3k times · Source

Is there anyway for this text input

<input type="text" aria-controls="existing-user-table" placeholder="Search">

to trigger an action on the controller when the user hits enter?

I don't like to enclose it in a form tag or create a button, just that input textfield.

Answer

Mike Grassotti picture Mike Grassotti · Sep 7, 2013

Use the {{input}} helper - if you include an action parameter it will trigger that action on the controller when the user hits enter. So:

{{input action="myAction" aria-controls="existing-user-table" placeholder="Search"}}

The input helper api docs do not mention this capability, but the helper just wraps Ember.TextField

Also it is possible to trigger the action on keyPress instead of enter by specifying the onEvent property:

{{input action="myAction" onEvent="keypress" aria-controls="existing-user-table" placeholder="Search"}}