Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 979
René Olivo Avatar asked Dec 05 '22 14:12

René Olivo


1 Answers

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.
  • like image 153
    Mudassir Ali Avatar answered Jan 12 '23 01:01

    Mudassir Ali