Paul Oliver
Paul Oliver

Reputation: 7681

Actions in Ember 2.5--route or controller?

I've been anticipating the end of controllers when routable components are coming in the future. So I've been putting my action handlers in my routes.

// app/something/route.js
actions: {
    doSomething() {
         alert('action handled in route');
    }
}

In my template:

{{!-- app/something/template.hbs --}}
{{some-component action="doSomething"}}

In my component:

{{!-- app/components/some-component/template.hbs --}}
<button {{action "onClickButton"}}>Click Me</button>

// app/components/some-component/component.js
actions: {
    onClickButton() {
       this.sendAction();
    }
}

Should I be handling actions in the route and avoiding controllers completely?

Upvotes: 2

Views: 1390

Answers (2)

Paul Oliver
Paul Oliver

Reputation: 7681

It appears that controllers are not going away, thanks to input from @locks on the Ember Learning Team.

According to his blog post:

Being future-proof does not mean never using any controller ever.

and

Generate controllers for all your routes.

You can even see in this git commit from trek that the Ember team is softening their stance on controllers.

So, in closing, controllers are fine to use. That's where I'm putting my actions.

Upvotes: 2

SamSebastien
SamSebastien

Reputation: 181

This is a really great package I have been using to solve this temporarily:

https://github.com/DockYard/ember-route-action-helper

A full write up on the rationale is here (https://dockyard.com/blog/2016/02/19/best-practices-route-actions), with the key point being:

"This addon gives you all the goodness of closure actions, and is a great way of taking steps to future proof your Ember application. When Routable Components do land, and actions work correctly, then upgrading your app simply becomes a search and replace for s/route-action/action."

Upvotes: 1

Related Questions