René Olivo
René Olivo

Reputation: 181

EmberJS: How to handle actions from the View not the Route / Controller

Today I have come to a shocking discovery: actions referenced on a view are handled by their route, not by the view which referenced it. Ex:

<a href="#" {{action edit}}>Edit this</a>

The edit action must be defined in the Route, not in the View. When I was not using a Router before the View was the one responsible for handling such events and I was really happy about it.

Can anyone please:

  1. explain to me why the Route must handle the event, and what are the benefits of this
  2. tell me how can I give control back to the View in matters of handling such actions/events?

Upvotes: 4

Views: 2208

Answers (1)

Mudassir Ali
Mudassir Ali

Reputation: 8041

Set the target as view

<a href="#" {{action edit target="view"}}>Edit this</a>

If your action is in controller then use

<a href="#" {{action edit}}>Edit this</a>

Default target refers to the view's controller

I'd suggest you to go through this Reference: Ember Action Helper

I'd like to mention some key points as per the above reference

  • In a typical Ember.Router-backed Application where views are managed through use of the {{outlet}} helper, actions will be forwarded to the current controller.

  • If the action is not defined in the controller, then the current route is targeted.

    Upvotes: 16

  • Related Questions