setupController method and transient controller


#1

I’m trying to use setupController method in the route to pass some data to the controller.

import Ember from 'ember';

export default Ember.Route.extend({
  setupController: function(controller,model) {
    this._super(controller,model);
    controller.var1 = "Variable 1";
  }
});

But the above doesn’t work if the controller isn’t a singleton. Is this by design?

SO question with some more information and twiddle link:


#2

I don’t have the perfect code but it gets the data from the posts controller. If all posts have the same author it does not do this again if you click trough the posts. I like to improve this code and remove the needs:.


#3

This appears to be an actual bug, since the instance of the controller is different from the instance you have in setupController and the one backing the view.

A workaround would be overriding the renderTemplate hook on your route to pass the instance of the controller versus a string reference which is looked up by default (and creating a new instance of the controller!).

export default Ember.Route.extend({
  setupController(controller, model) {
    this._super(...arguments);
    controller.set('var1', 'foo');
  },
  renderTemplate(controller, model) {
    // note: don't call super here
    this.render('application', {
      controller: controller,
      model: model
    });
  }
});

Where I believe the bug is:

It’s not passing the instance of the controller, instead it’s forced to take a different code path and generates a new controller:


#4

Also I reported the bug here: https://github.com/emberjs/ember.js/issues/11995

My workaround will suffice though until a fix lands. It’s possible it won’t be fixed though given the focus to move towards routeable components.