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

Support defining injections to occur after other injections #1340

Merged
merged 1 commit into from Sep 10, 2012
Jump to file or symbol
Failed to load files and symbols.
+124 −26
Diff settings

Always

Just for now

@@ -308,9 +308,8 @@ Ember.Application = Ember.Namespace.extend(
router.get('postsController.router') // router
*/
initialize: function(router) {
var properties = Ember.A(Ember.keys(this)),
injections = get(this.constructor, 'injections'),
namespace = this, controller, name;
var injections = get(this.constructor, 'injections'),
namespace = this, properties;
if (!router && Ember.Router.detect(namespace['Router'])) {
router = namespace['Router'].create();
@@ -329,9 +328,30 @@ Ember.Application = Ember.Namespace.extend(
set(router, 'namespace', this);
}
// Sort injections by their befores and afters.
// NOTE: Injections cannot have both a before and an after.
injections.sort(function(a, b) {
if (a.before) {
if (!b.before || a.before === b.name) { return -1; }
} else if (a.after) {
if (!b.after || a.after === b.name) { return 1; }
}
if (b.before) {
if (!a.before || b.before === a.name) { return 1; }
} else if (b.after) {
if (!a.after || b.after === a.name) { return -1; }
}
return 0;
});
injections.forEach(function(injection) {
// Grab properties before each injection, in case an injection added
// properties to the namespace.
properties = Ember.A(Ember.keys(namespace));
properties.forEach(function(property) {
injection[1](namespace, router, property);
injection.injection(namespace, router, property);
});
});
@@ -408,23 +428,14 @@ Ember.Application = Ember.Namespace.extend(
Ember.Application.reopenClass({
concatenatedProperties: ['injections'],
injections: Ember.A(),
registerInjection: function(options) {
var injections = get(this, 'injections'),
before = options.before,
name = options.name,
injection = options.injection,
location;
if (before) {
location = injections.find(function(item) {
if (item[0] === before) { return true; }
});
location = injections.indexOf(location);
} else {
location = get(injections, 'length');
}
registerInjection: function(injection) {
var injections = get(this, 'injections');
Ember.assert("The injection '" + injection.name + "' has already been registered", !injections.findProperty('name', injection.name));
Ember.assert("An injection cannot be registered with both a before and an after", !(injection.before && injection.after));
Ember.assert("An injection cannot be registered without an injection function", Ember.canInvoke(injection, 'injection'));
injections.splice(location, 0, [name, injection]);
injections.push(injection);
}
});
@@ -219,37 +219,124 @@ test("injections can be registered in a specified order", function() {
var oldInjections = Ember.Application.injections;
var firstInjectionCalled = 0,
secondInjectionCalled = 0;
secondInjectionCalled = 0,
thirdInjectionCalled = 0,
fourthInjectionCalled = 0,
fifthInjectionCalled = 0;
Ember.Application.injections = Ember.A();
Ember.Application.registerInjection({
name: 'fourth',
after: 'third',
injection: function() {
ok(firstInjectionCalled > 1, "fourth: first injection should have been called");
ok(secondInjectionCalled > 1, "fourth: second injection should have been called");
ok(thirdInjectionCalled > 1, "fourth: third injection should have been called");
ok(fifthInjectionCalled === 0, "fourth: fifth injection should not have been called yet");
fourthInjectionCalled++;
}
});
Ember.Application.registerInjection({
name: 'second',
before: 'third',
injection: function() {
ok(firstInjectionCalled > 0, 'first injection should be called first');
ok(firstInjectionCalled > 1, "second: first injection should have been called");
ok(thirdInjectionCalled === 0, "second: third injection should not have been called yet");
ok(fourthInjectionCalled === 0, "second: fourth injection should not have been called yet");
ok(fifthInjectionCalled === 0, "second: fifth injection should not have been called yet yet");
secondInjectionCalled++;
}
});
Ember.Application.registerInjection({
name: 'fifth',
after: 'fourth',
injection: function() {
ok(firstInjectionCalled > 1, "fifth: first injection should have been called");
ok(secondInjectionCalled > 1, "fifth: second injection should have been called");
ok(thirdInjectionCalled > 1, "fifth: third injection should have been called");
ok(fourthInjectionCalled > 1, "fifth: fourth injection should have been called");
fifthInjectionCalled++;
}
});
Ember.Application.registerInjection({
name: 'first',
before: 'second',
injection: function() {
ok(secondInjectionCalled === 0, "first: second injection should not have been called yet");
ok(thirdInjectionCalled === 0, "first: third injection should not have been called yet");
ok(fourthInjectionCalled === 0, "first: fourth injection should not have been called yet");
ok(fifthInjectionCalled === 0, "first: fifth injection should not have been called yet yet");
firstInjectionCalled++;
ok(secondInjectionCalled === 0, "second injection should not have been called yet");
},
before: 'second'
}
});
Ember.Application.registerInjection({
name: 'third',
injection: function() {
ok(firstInjectionCalled > 1, "third: first injection should have been called");
ok(secondInjectionCalled > 1, "third: second injection should have been called");
ok(fourthInjectionCalled === 0, "third: fourth injection should not have been called yet");
ok(fifthInjectionCalled === 0, "third: fifth injection should not have been called yet yet");
thirdInjectionCalled++;
}
});
var router;
Ember.run(function() {
app = Ember.Application.create({
rootElement: '#qunit-fixture'
});
expect(get(Ember.keys(app), 'length') * 25);
router = Ember.Object.create();
app.initialize(router);
});
Ember.run(function() {
router.destroy();
});
Ember.Application.injections = oldInjections;
});
test("injections are passed properties created from previous injections", function() {
var oldInjections = Ember.Application.injections;
var secondInjectionWasPassedProperty = false;
Ember.Application.injections = Ember.A();
Ember.Application.registerInjection({
name: 'first',
injection: function(app, router, property) {
app.set('foo', true);
}
});
Ember.Application.registerInjection({
name: 'second',
after: 'first',
injection: function(app, router, property) {
if (property === 'foo') {
secondInjectionWasPassedProperty = true;
}
}
});
var router;
Ember.run(function() {
app = Ember.Application.create({
rootElement: '#qunit-fixture'
});
expect(get(Ember.keys(app), 'length') * 2);
router = Ember.Object.create();
app.initialize(router);
});
ok(secondInjectionWasPassedProperty, "second injections wasn't passed the property created in the first");
Ember.run(function() {
router.destroy();
});
ProTip! Use n and p to navigate between commits in a pull request.