Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EmberJS: How to transition to a router from a controller's action

I have an action:

{{action create target="controller"}}

which I have targeted to the bound controller (rather than the router) like this:

App.AddBoardController = Ember.Controller.extend
    create: ->
        App.store.createRecord App.Board, {title: @get "boardName"}
        App.store.commit()
        //TODO: Redirect to route

How do I redirect back to a route from the controller action?

like image 695
Charlie Avatar asked Jul 19 '12 00:07

Charlie


People also ask

Can you transition in Ember?

During a route transition, the Ember Router passes a transition object to the various hooks on the routes involved in the transition. Any hook that has access to this transition object has the ability to immediately abort the transition by calling transition.

What is route in Ember?

This is the core feature of the Ember. js. The router used for to translate URL into the series of templates and also it represents the state of an application. The Ember. js uses the HashChange event that helps to know change of route; this can be done by implementing HashLocation object.

What is a controller and route?

A Controller is routable object which receives a single property from the Route – model – which is the return value of the Route's model() method. The model is passed from the Route to the Controller by default using the setupController() function.

What is the prime task performed by controllers in Ember JS?

What are the prime tasks that are performed by controllers in Ember. js? Decorating the model which is returned by the route is a very essential task that needs to be performed in Ember.


2 Answers

Use transitionToRoute('route') to redirect inside an Ember controller action:

App.AddBoardController = Ember.Controller.extend({
    create: function(){ 
        ...
        //TODO: Redirect to route
        this.transitionToRoute('route_name');
    }
...
like image 146
HaoQi Li Avatar answered Oct 15 '22 18:10

HaoQi Li


In fact, this is not Ember idiomatic. From what I know, and what I have learnt from Tom Dale himself, here are some remarks about that code:

  • First, you should not transitionTo from elsewhere than inside the router: by doing so, you are exposing yourself to serious issues as you don't know in which state is the router, so to keep stuff running, you will quickly have to degrade your design, and by the way the overall quality of you code, and finally the stability of your app,
  • Second, the action content you are showing should be located inside the router to avoid undesired context execution. The router is indeed a way to enforce a coherent behavior for the whole app, with actions being processed only in certain states. While you are putting the actions implementation into Controllers, those actions can be called at anytime, any including wrong...
  • Finally, Ember's controllers are not aimed to contain behavior as they rather are value-added wrappers, holding mainly computed properties. If you nevertheless want to factorize primitives, maybe the model can be a good place, or a third party context, but certainly not the Controller.

You should definitely put the action inside the router, and transitionTo accordingly.

Hope this will help.

UPDATE

First example (close to your sample)

In the appropriated route:

saveAndReturnSomewhere: function (router, event) {
  var store = router.get('store'),
      boardName = event.context; // you pass the (data|data container) here. In the view: {{action saveAndReturnSomewhere context="..."}}
  store.createRecord(App.Board, {
    title: boardName
  });
  store.commit();
  router.transitionTo('somewhere');
}

Refactored example

I would recommend having the following routes:

  • show: displays an existing item,
  • edit: proposes to input item's fields

Into the enclosing route, following event handlers:

  • createItem: create a new record and transitionTo edit route, e.g
  • editItem: transitionTo edit route

Into the edit route, following event handlers:

  • saveItem: which will commit store and transitionTo show route, e.g
like image 26
Mike Aski Avatar answered Oct 15 '22 16:10

Mike Aski