Question

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

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top