1

I want to set a property of ApplicationController from within App.ready. But I don't see how this can be done from within this function

App.ready = function() {
    this.controller.set("isLoaded", true) ;
}

Is something like this possible ?

CHeers

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333

1 Answers1

3

It's not really possible to set controller values directly if the objects aren't directly linked (Router > Controller > View), but I would also question why you wish to set something in the ApplicationController from the ready event. There are two options that you can take. Either you can create a new Ember object and set a value from the ready event there, and have the controller observe that property (example here. The second option is to respond to the 'didInsertElement' event on the ApplicationView (an example is here)

Option 1:

App = Em.Application.create({
    ready: function () {
        App.SomeObject.set('someValue', 'A value');
        alert('ready event fired');
    }
});

App.SomeObject = Em.Object.create({
    someValue: '1'
});

App.ApplicationController = Em.Controller.extend({
    someFunction: function () {
        // do something here (note that the original value of 1 for someValue is never set as the application
        // overwrites it immediately
    }.observes('App.SomeObject.someValue')
});

App.ApplicationView = Em.View.extend({    
    didInsertElement : function () {
        alert('view has been inserted into the dom');
        alert(App.SomeObject.get('someValue'));
    }
});

Option 2:

window.App = Em.Application.create({
    ready: function () {
        alert('app is ready');
    }
});

App.ApplicationController = Em.Controller.extend();

App.ApplicationView = Em.View.extend({    
    didInsertElement : function () {
        alert('view has been inserted into the dom');
    }
});

EDIT: Note that I'm pretty sure option 1 is messy and will be frowned upon by the Ember devs (although I can't say for certain).

DF_
  • 3,743
  • 25
  • 34
  • thnx a lot! good point! What I need is to delay the rendering process. I want to show my spinner a little bit langer than 0.01s! I can of course render everything inside an element which has **display:none** initially and with your option 1 remove this. Or is there something better ? – Jeanluca Scaljeri Feb 06 '13 at 19:45
  • I think if you wanted to delay showing the view, then the cleanest way to do that would be to do a `setTimeout(..., 1000)` on the didInsertElement function of the view. Then after one second or so you can reset the classbinding. I quickly modified this [jsfiddle](http://jsfiddle.net/nv7y5/4/) as an example. – DF_ Feb 06 '13 at 23:24
  • thnx again, that looks like the way to go! – Jeanluca Scaljeri Feb 07 '13 at 07:31