Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX stable] Instance initializers should run before App.ready #11289

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions packages/ember-application/lib/system/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,13 @@ var Application = Namespace.extend(DeferredMixin, {
// tamper with the default `Ember.Router`.
// 2.0TODO: Can we move this into a globals-mode-only library?
this.Router = (this.Router || Router).extend();
this.waitForDOMReady(this.buildDefaultInstance());
this.buildDefaultInstance();
this.waitForDOMReady();
}
} else {
this.Router = (this.Router || Router).extend();
this.waitForDOMReady(this.buildDefaultInstance());
this.buildDefaultInstance();
this.waitForDOMReady();
}
},

Expand Down Expand Up @@ -357,13 +359,13 @@ var Application = Namespace.extend(DeferredMixin, {
loading.

@private
@method scheduleInitialize
@method waitForDOMReady
*/
waitForDOMReady(_instance) {
waitForDOMReady() {
if (!this.$ || this.$.isReady) {
run.schedule('actions', this, 'domReady', _instance);
run.schedule('actions', this, 'domReady');
} else {
this.$().ready(run.bind(this, 'domReady', _instance));
this.$().ready(run.bind(this, 'domReady'));
}
},

Expand Down Expand Up @@ -552,16 +554,12 @@ var Application = Namespace.extend(DeferredMixin, {
to defer readiness until the auth token has been retrieved.

@private
@method _initialize
@method domReady
*/
domReady(_instance) {
domReady() {
if (this.isDestroyed) { return; }

var app = this;

this.boot().then(function() {
app.runInstanceInitializers(_instance);
});
this.boot();

return this;
},
Expand Down Expand Up @@ -718,6 +716,7 @@ var Application = Namespace.extend(DeferredMixin, {
this.__deprecatedInstance__.setupEventDispatcher();
}

this.runInstanceInitializers(this.__deprecatedInstance__);
this.ready(); // user hook
this.__deprecatedInstance__.startRouting();

Expand All @@ -734,8 +733,8 @@ var Application = Namespace.extend(DeferredMixin, {
},

/**
Called when the Application has become ready.
The call will be delayed until the DOM has become ready.
Called when the Application has become ready, immediately before routing
begins. The call will be delayed until the DOM has become ready.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See line 186 ⬆️


@event ready
*/
Expand Down
35 changes: 35 additions & 0 deletions packages/ember-application/tests/system/application_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import View from "ember-views/views/view";
import Controller from "ember-runtime/controllers/controller";
import NoneLocation from "ember-routing/location/none_location";
import EmberObject from "ember-runtime/system/object";
import EmberRoute from "ember-routing/system/route";
import jQuery from "ember-views/system/jquery";
import compile from "ember-template-compiler/system/compile";

Expand Down Expand Up @@ -127,6 +128,40 @@ QUnit.test('initialized application go to initial route', function() {
equal(jQuery('#qunit-fixture h1').text(), "Hi from index");
});

QUnit.test("ready hook is called before routing begins", function() {
expect(2);

run(function() {
function registerRoute(application, name, callback) {
var route = EmberRoute.extend({
activate: callback
});

application.register('route:' + name, route);
}

var MyApplication = Application.extend({
ready() {
registerRoute(this, 'index', function() {
ok(true, 'last-minite route is activated');
});
}
});

app = MyApplication.create({
rootElement: '#qunit-fixture'
});

app.Router.reopen({
location: 'none'
});

registerRoute(app, 'application', function() {
ok(true, 'normal route is activated');
});
});
});

QUnit.test("initialize application via initialize call", function() {
run(function() {
app = Application.create({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,33 @@ if (Ember.FEATURES.isEnabled('ember-application-instance-initializers')) {
});
});

QUnit.test("initializers are run before ready hook", function() {
expect(2);

var readyWasCalled = false;

var MyApplication = Application.extend({
ready() {
ok(true, 'ready is called');
readyWasCalled = true;
}
});

MyApplication.instanceInitializer({
name: 'initializer',
initialize() {
ok(!readyWasCalled, 'ready is not yet called');
}
});

run(function() {
app = MyApplication.create({
router: false,
rootElement: '#qunit-fixture'
});
});
});

if (initializeContextFeatureEnabled) {
QUnit.test("initializers should be executed in their own context", function() {
expect(1);
Expand Down