Is it considered bad practice to transitionToRoute from a controller?

Hi,

as Mike Aski answered on this question at stackoverflow:

its considered bad practice to transition to another route from a controller.

  • is it true?

  • if so, what will be a better approach?

      App.CreateReviewController = Ember.Controller.extend({
        createReview : function () {
          var review = App.Review.createRecord({
          author : this.get('author'),
          text : this.get('text')
        });
    
        review.save();
    
        // transition from the controller, is it bad?
        this.transitionToRoute('reviews');
      }});
    

Thank you in advance.

I personally don’t find anything wrong with this, however just so you know you can also put this into the route in the events hash and use transitionTo.

Hi, @jonnii could you show me how to use the route’s events hash?

But trnasitionTo is deprecated, isn’t it?

Use of transitionTo in the controllers is deprecated. If you want to do a transition from a controllers you should use transitionToRoute and if you need to transition from one route to another you should use transitionTo

2 Likes

I do this all the time in my app. I frequently end up navigating users way after a save or other async action has happened inside of a controller. It seems silly to send an event to the router only to have the router just call transition, when you could just transition from the controller. Just my $0.02.

I like that separation of concerns.

  • the controller only cares about saving and notify about it. it should not care about routing. it’s not its job.
  • the router listen to the event and doing its routing related staff. it does not care who trigger the event.

Like that each part of the application kept isolated and focused only on its job.

3 Likes