Skip to content

Commit

Permalink
WIP support service injection with namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
iezer committed Feb 5, 2018
1 parent e749979 commit 6f4ea49
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 9 deletions.
13 changes: 10 additions & 3 deletions packages/container/lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,15 @@ if (DEBUG) {

for (let key in hash) {
if (hash.hasOwnProperty(key)) {
let { fullName } = parseInjectionString(hash[key]);
let injection = hash[key];
let injectionString = (typeof injection === 'string') ? injection : injection.fullName;
let { fullName } = parseInjectionString(injectionString);
assert(`Expected a proper full name, given '${fullName}'`, this.isValidFullName(fullName));

injections.push({
property: key,
fullName: hash[key]
fullName: hash[key].fullName,
namespace: hash[key].namespace
});
}
}
Expand All @@ -647,10 +650,14 @@ if (DEBUG) {
if (!injections) { return; }

for (let i = 0; i < injections.length; i++) {
let injection = injections[i];
let injectionString = (typeof injection === 'string') ? injection : injection.fullName;
let {
fullName,
namespace
} = parseInjectionString(injections[i].fullName);
} = parseInjectionString(injectionString);

namespace = namespace || injection.namespace;

assert(`Attempting to inject an unknown injection: '${fullName}'`, this.has(fullName, {namespace}));
}
Expand Down
5 changes: 3 additions & 2 deletions packages/container/tests/container_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ moduleFor('Container', class extends AbstractTestCase {

Apple.reopenClass({
_lazyInjections() {
return ['orange:main', 'banana:main'];
return [{ fullName: 'orange:main' },
{ fullName: 'banana:main' }];
}
});

Expand All @@ -423,7 +424,7 @@ moduleFor('Container', class extends AbstractTestCase {
Apple.reopenClass({
_lazyInjections: () => {
assert.ok(true, 'should call lazy injection method');
return ['orange:main'];
return [{ fullName: 'orange:main' }];
}
});

Expand Down
6 changes: 5 additions & 1 deletion packages/ember-metal/lib/injected_property.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import { lookupWithRawString } from 'container';
to the property's name
@private
*/
export default function InjectedProperty(type, name) {
export default function InjectedProperty(type, name, options) {
this.type = type;
this.name = name;
this.options = options;

this._super$Constructor(injectedPropertyGet);
AliasedPropertyPrototype.oneWay.call(this);
Expand All @@ -37,6 +38,9 @@ function injectedPropertyGet(keyName) {
assert(`InjectedProperties should be defined with the inject computed property macros.`, desc && desc.type);
assert(`Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container.`, owner);

if (desc.options && desc.options.namespace) { // && (!desc.name || desc.name.indexOf('::') === -1)
return lookupWithRawString(owner, desc.type, `${desc.options.namespace}::${desc.name || keyName}`);
}
return lookupWithRawString(owner, desc.type, desc.name || keyName);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ember-runtime/lib/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const typeValidators = {};
export function createInjectionHelper(type, validator) {
typeValidators[type] = validator;

inject[type] = name => new InjectedProperty(type, name);
inject[type] = (name, options) => new InjectedProperty(type, name, options);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion packages/ember-runtime/lib/system/core_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,10 @@ if (DEBUG) {
for (key in proto) {
desc = descriptorFor(proto, key);
if (desc instanceof InjectedProperty) {
injections[key] = `${desc.type}:${desc.name || key}`;
injections[key] = {
fullName: `${desc.type}:${desc.name || key}`,
namespace: desc.options && desc.options.namespace
};
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/ember-runtime/tests/inject_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ if (DEBUG) {
bar: new InjectedProperty('quux')
});

assert.deepEqual(AnObject._lazyInjections(), { 'foo': 'foo:bar', 'bar': 'quux:bar' }, 'should return injected container keys');
assert.deepEqual(AnObject._lazyInjections(), {
'foo': { fullName: 'foo:bar', namespace: undefined },
'bar': { fullName: 'quux:bar', namespace: undefined }
}, 'should return injected container keys');
});
}
18 changes: 18 additions & 0 deletions packages/ember/tests/service_injection_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,23 @@ if (EMBER_MODULE_UNIFICATION) {
assert.ok(controller.get('myService') instanceof MyService);
});
}

['@test Service with namespace can be injected with namespace and is resolved'](assert) {
let namespace = 'my-namespace';
this.add('controller:application', Controller.extend({
myService: inject.service('my-service', { namespace })
}));
let MyService = Service.extend();
this.add({
specifier: 'service:my-service',
namespace
}, MyService);

this.visit('/').then(() => {
let controller = this.applicationInstance.lookup('controller:application');

assert.ok(controller.get('myService') instanceof MyService);
});
}
});
}

0 comments on commit 6f4ea49

Please sign in to comment.