0

I have a case were I need to choose the template of the view based on the initial property value of the controller. Thus I need to access the controller while I am inside the init hook of the view but when i access the controller it returns "null".

MyApp.ApplicationController = Em.Controller.extend({
  templateVersion: 'small'
});

MyApp.ApplicationView = Em.View.extend({
  init: function(){
    this._super();
    console.log('Controller is: ',this.get('controller'));
    if(this.get('controller').get('templateVersion') == 'small')
    {
      this.set('templateName', 'application-small');
    } else {
      this.set('templateName', 'application-bigger');
    }
  }
});

This is not the real case but an example for the real scenario. For an example I have setup a jsbin here

1 Answer 1

2

I guess a more appropriate way of doing this would be by determine dynamically the templateName, something like the following:

MyApp.ApplicationView = Ember.View.extend({
  templateName: function() {
    if (this.get("controller.templateVersion") == "small") {
        return "application-small";
    } else {
        return "application-bigger";
    }
  }.property('controller.templateVersion')
});

Doing it this way you dont need to hook into the init function and thus not having your controller properties available.

Here your updated jsbin.

Update

After your last comment I realized that the delay is the important part to make your use case work, here is an improved version which indeed changes even if the templateVersion is initially not defined and get's setted with some delay, this time we observe the templateName property of the view and invoke a rerender.

MyApp.ApplicationView = Ember.View.extend({
  templateName: function() {
    if (this.get("controller.templateVersion") == "small") {
      return "application-small";
    } else {
      return "application-bigger";
    }
  }.property('controller.templateVersion'),
  templateChanged: function() {
    this.rerender();
  }.observes('templateName')
});

And here another jsbin with the new version with a simulated delay of 2 seconds, but it could be whatever value.

Hope it helps.

5
  • would it work even if there is some delay in templateVersion value fetching?
    – guleria
    Aug 2, 2013 at 8:54
  • I guess yes, since it's a computed property, but sincerely haven't tested this use case Aug 2, 2013 at 9:15
  • not sure but I guess it won't work because ember uses templateName property to search for a template for a view and once done changing, the template name doesn't affect the template as it is not recalculated and re-rendered. Thus whatever name it gets in the beginning is set as its template. Irrespective of the change later.
    – guleria
    Aug 2, 2013 at 9:48
  • 1
    @guleria, I've updated my answer to fit your use case more closely, have a look. Aug 2, 2013 at 10:56
  • that fitted exactly to my case. Thanks a lot.
    – guleria
    Aug 2, 2013 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.