Seth
Seth

Reputation: 2796

Migrating to Ember.Router without Breaking Existing View-based {{action}} Handlers

I have a codebase written against emberjs pre-1.0, without the use of Ember.Router or Ember.StateManager. I'm trying to migrate to Ember.Router, iteratively if possible.

I created a skeleton App.ApplicationController, App.ApplicationView, and App.Router (with only one route). App.ApplicationView simply wraps my existing entry-point view: App.PreRouterMainView. Everything loaded OK, except {{action}} bindings on existing views no longer worked:

I frequently use {{action "someTargetMethod"}} to invoke PreRouterChildView.someTargetMethod(), but after embedding/'routifying' PreRouterMainView, the someTargetMethod() is now invoked on the router instead.

Is there a simple way to have PreRouterMainView and child views continue to work "the old way", with events invoking methods on the closest View instead of the router?

Or do I need to refactor all my view-methods to router-methods in one big change? I'd really rather to this iteratively, if possible.

Upvotes: 1

Views: 328

Answers (1)

buuda
buuda

Reputation: 1427

You will have to refactor your actions to explicitly target the view (or any other object on a valid path):

{{action someTargetMethod target="view"}}

However, the default context of actions has been changed to the router because the router should handle most actions, especially the ones changing state/route. You can call methods on your controller and view (through the event context) in the router method handling the action. If the action is not changing state or data then having the view handle it is fine.

EDIT: The inline code documentation indicates that you can change the default action target by setting a property on the controller:

### Specifying a Target
There are several possible target objects for `{{action}}` helpers:

In a typical `Ember.Router`-backed Application where views are managed
through use of the `{{outlet}}` helper, actions will be forwarded to the
current state of the Applications's Router. See Ember.Router 'Responding
to User-initiated Events' for more information.

If you manaully set the `target` property on the controller of a template's
`Ember.View` instance, the specifed `controller.target` will become the target
for any actions. Likely custom values for a controller's `target` are the
controller itself or a StateManager other than the Application's Router.

If the templates's view lacks a controller property the view itself is the target.

Upvotes: 4

Related Questions