Skip to content

Commit 1d48dad

Browse files
committed
Merge pull request #10179 from gf3/initializer-context
Execute initializers in their respective context
2 parents ebe8840 + 90cbc09 commit 1d48dad

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

FEATURES.md

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ for a detailed explanation.
55

66
## Feature Flags
77

8+
* `ember-application-initializer-context`
9+
10+
Sets the context of the initializer function to its object instead of the
11+
global object.
12+
13+
Added in [#10179](https://github.com/emberjs/ember.js/pull/10179).
14+
815
* `ember-testing-checkbox-helpers`
916

1017
Add `check` and `uncheck` test helpers.

features.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"new-computed-syntax": null,
1515
"ember-testing-checkbox-helpers": null,
1616
"ember-metal-stream": null,
17-
"ember-htmlbars-each-with-index": true
17+
"ember-htmlbars-each-with-index": true,
18+
"ember-application-initializer-context": null
1819
},
1920
"debugStatements": [
2021
"Ember.warn",

packages/ember-application/lib/system/application.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -695,13 +695,19 @@ var Application = Namespace.extend(DeferredMixin, {
695695

696696
for (var i = 0; i < initializers.length; i++) {
697697
initializer = initializersByName[initializers[i]];
698-
graph.addEdges(initializer.name, initializer.initialize, initializer.before, initializer.after);
698+
graph.addEdges(initializer.name, initializer, initializer.before, initializer.after);
699699
}
700700

701701
graph.topsort(function (vertex) {
702702
var initializer = vertex.value;
703703
Ember.assert("No application initializer named '" + vertex.name + "'", !!initializer);
704-
initializer(registry, namespace);
704+
705+
if (Ember.FEATURES.isEnabled("ember-application-initializer-context")) {
706+
initializer.initialize(registry, namespace);
707+
} else {
708+
var ref = initializer.initialize;
709+
ref(registry, namespace);
710+
}
705711
});
706712
},
707713

packages/ember-application/tests/system/initializers_test.js

+22
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,25 @@ test("initializers are per-app", function() {
239239
initialize: function(registry) {}
240240
});
241241
});
242+
243+
if (Ember.FEATURES.isEnabled("ember-application-initializer-context")) {
244+
test("initializers should be executed in their own context", function() {
245+
expect(1);
246+
var MyApplication = Application.extend();
247+
248+
MyApplication.initializer({
249+
name: 'coolBabeInitializer',
250+
myProperty: 'coolBabe',
251+
initialize: function(registry, application) {
252+
equal(this.myProperty, 'coolBabe', 'should have access to its own context');
253+
}
254+
});
255+
256+
run(function() {
257+
app = MyApplication.create({
258+
router: false,
259+
rootElement: '#qunit-fixture'
260+
});
261+
});
262+
});
263+
}

0 commit comments

Comments
 (0)