DEV Community

Michal Bryxí
Michal Bryxí

Posted on

4 1

Ember {{on}} vs {{action}} caveat

Today I learned one important difference between the traditional way of invoking actions in Ember via an action helper:

<button {{action "submit"}}>
  Click me!
</button>
Enter fullscreen mode Exit fullscreen mode

And via the new on element modifier:

<button {{on "click" this.submit}}>
  Click me!
</button>
Enter fullscreen mode Exit fullscreen mode

The subtle, yet important thing is that action helper prevents default browser action, but on element modifier does not.

Why is this important? If you'd have the buttons from the example above inside a <form> tag, then the first button will work as expected (action will be triggered), but second button will cause an unexpected full page reload on click.

Ways to fix

Two easy fixes come to my mind:

Either you can change the type of the button so that it does not trigger a submit:

<form>
  <button type="button" {{on "click" this.submit}}>
    Click me!
  </button>
</form>
Enter fullscreen mode Exit fullscreen mode

Or you can use ember-event-helpers addon.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series