Permalink
Browse files

Add autoinit flag to Application to call initialize on DOM ready.

Application autoinit defaults to true if not testing.
  • Loading branch information...
krisselden committed Sep 17, 2012
1 parent 7e491dc commit 1c7cf46a866a545f68192ab588ee93a1ea160e00
@@ -245,6 +245,10 @@ Ember.Application = Ember.Namespace.extend(
*/
customEvents: null,
autoinit: !Ember.testing,
isInitialized: false,
init: function() {
if (!this.$) { this.$ = Ember.$; }
@@ -257,6 +261,14 @@ Ember.Application = Ember.Namespace.extend(
this._readinessDeferrals = 1;
this.waitForDOMContentLoaded();
if (this.autoinit) {
var self = this;
this.$().ready(function() {
if (self.isDestroyed || self.isInitialized) return;
self.initialize();

This comment has been minimized.

Show comment
Hide comment
@trek

trek Sep 20, 2012

Member

This line requires a bunch of documentation changes. I'm starting to see complaints from people using ember-latest and reading docs inline.

@trek

trek Sep 20, 2012

Member

This line requires a bunch of documentation changes. I'm starting to see complaints from people using ember-latest and reading docs inline.

});
}
},
/** @private */
@@ -315,12 +327,17 @@ Ember.Application = Ember.Namespace.extend(
@param router {Ember.Router}
*/
initialize: function(router) {
Ember.assert("Application initialize may only be call once", !this.isInitialized);

This comment has been minimized.

Show comment
Hide comment
@ngirardin

ngirardin Oct 11, 2012

Little typo here, "call" should be replaced by "called"

@ngirardin

ngirardin Oct 11, 2012

Little typo here, "call" should be replaced by "called"

Ember.assert("Application not destroyed", !this.isDestroyed);
router = this.setupRouter(router);
this.runInjections(router);
Ember.runLoadHooks('application', this);
this.isInitialized = true;
// At this point, any injections or load hooks that would have wanted
// to defer readiness have fired.
this.advanceReadiness();
@@ -86,6 +86,7 @@ var app;
module("Ember.Application initialization", {
teardown: function() {
Ember.TEMPLATES = {};
Ember.run(function(){ app.destroy(); });
}
});
@@ -289,3 +290,48 @@ test("ControllerObject class can be initialized with target, controllers and vie
equal(app.get('router.postController.controllers') instanceof Ember.StateManager, true, "controller has controllers");
equal(app.get('router.postController.view') instanceof Ember.View, true, "controller has view");
});
test("Application initialized twice raises error", function() {
Ember.run(function() {
app = Ember.Application.create({
rootElement: '#qunit-fixture'
}).initialize();
});
raises(function(){
Ember.run(function() {
app.initialize();
});
}, Error, 'raises error');
});
test("Minimal Application initialized with just an application template", function() {
Ember.$('#qunit-fixture').html('<script type="text/x-handlebars">Hello World</script>');
Ember.run(function () {
app = Ember.Application.create({
rootElement: '#qunit-fixture'
}).initialize();
});
equal(Ember.$('#qunit-fixture').text(), 'Hello World');
});
test("Minimal Application initialized with an application template and injections", function() {
Ember.$('#qunit-fixture').html('<script type="text/x-handlebars">Hello {{controller.name}}!</script>');
Ember.run(function () {
app = Ember.Application.create({
rootElement: '#qunit-fixture'
});
});
app.ApplicationController = Ember.Controller.extend({name: 'Kris'});
Ember.run(function () {
// required to receive injections
var stateManager = Ember.Object.create();
app.initialize(stateManager);
});
equal(Ember.$('#qunit-fixture').text(), 'Hello Kris!');
});
@@ -1,5 +1,5 @@
var jQuery, Application, application;
var readyWasCalled, domReady, readyCallback;
var readyWasCalled, domReady, readyCallbacks;
// We are using a small mock of jQuery because jQuery is third-party code with
// very well-defined semantics, and we want to confirm that a jQuery stub run
@@ -9,10 +9,11 @@ var readyWasCalled, domReady, readyCallback;
module("Application readiness", {
setup: function() {
readyWasCalled = 0;
readyCallbacks = [];
var jQueryInstance = {
ready: function(callback) {
readyCallback = callback;
readyCallbacks.push(callback);
if (jQuery.isReady) {
domReady();
}
@@ -28,7 +29,10 @@ module("Application readiness", {
domReady = function() {
if (domReadyCalled !== 0) { return; }
domReadyCalled++;
readyCallback();
var i;
for (i=0; i<readyCallbacks.length; i++) {
readyCallbacks[i]();
}
};
Application = Ember.Application.extend({
@@ -81,6 +85,22 @@ test("Ember.Application's ready event is called after the document becomes ready
equal(readyWasCalled, 1, "ready was called now that DOM is ready");
});
test("Ember.Application's ready event is called after the document becomes ready without initialize if autoinit is set", function() {
Ember.run(function() {
application = Application.create({
autoinit: true
});
});
equal(readyWasCalled, 0, "ready wasn't called yet");
Ember.run(function() {
domReady();
});
equal(readyWasCalled, 1, "ready was called now that DOM is ready");
});
test("Ember.Application's ready event can be deferred by other components", function() {
Ember.run(function() {
application = Application.create();

6 comments on commit 1c7cf46

@lipp

This comment has been minimized.

Show comment
Hide comment
@lipp

lipp Sep 19, 2012

Am I supposed to call App.initialize() explicitly (as some days ago)?

lipp replied Sep 19, 2012

Am I supposed to call App.initialize() explicitly (as some days ago)?

@garth

This comment has been minimized.

Show comment
Hide comment
@garth

garth Oct 3, 2012

Contributor

This commit caused us problems. We had to modify our html as follows to work around.

  <script>
    Ember = { testing: true }
  </script>
  <script src="/js/ember.js"></script>
Contributor

garth replied Oct 3, 2012

This commit caused us problems. We had to modify our html as follows to work around.

  <script>
    Ember = { testing: true }
  </script>
  <script src="/js/ember.js"></script>
@sly7-7

This comment has been minimized.

Show comment
Hide comment
@sly7-7

sly7-7 Oct 3, 2012

Contributor

Does it work if you simply remove the call to App.initialize() ?

Contributor

sly7-7 replied Oct 3, 2012

Does it work if you simply remove the call to App.initialize() ?

@ngirardin

This comment has been minimized.

Show comment
Hide comment
@ngirardin

ngirardin Oct 11, 2012

I'm sorry but I don't get it, when using the code below as before I got an Uncaught TypeError: Object (subclass of Ember.Application) has no method 'initialize' error.

window.App = Ember.Application.extend({
    rootElement: 'div.modal-body'
});

App.Router = Ember.Router.extend({
    root: Ember.Route.extend({
        ...
    })
});

...

App.initialize();

When removing App.initialize() my application don't start. What is the new correct usage of App.initialize?

Thanks

ngirardin replied Oct 11, 2012

I'm sorry but I don't get it, when using the code below as before I got an Uncaught TypeError: Object (subclass of Ember.Application) has no method 'initialize' error.

window.App = Ember.Application.extend({
    rootElement: 'div.modal-body'
});

App.Router = Ember.Router.extend({
    root: Ember.Route.extend({
        ...
    })
});

...

App.initialize();

When removing App.initialize() my application don't start. What is the new correct usage of App.initialize?

Thanks

@sly7-7

This comment has been minimized.

Show comment
Hide comment
@sly7-7

sly7-7 Oct 11, 2012

Contributor

You have to create an instance of Ember.Application, not a class that extends it :)

Contributor

sly7-7 replied Oct 11, 2012

You have to create an instance of Ember.Application, not a class that extends it :)

@ngirardin

This comment has been minimized.

Show comment
Hide comment
@ngirardin

ngirardin Oct 12, 2012

You were right, it works, I tried it before but without success... I did have missed something, so thanks!

ngirardin replied Oct 12, 2012

You were right, it works, I tried it before but without success... I did have missed something, so thanks!

Please sign in to comment.