diff --git a/packages/container/lib/registry.js b/packages/container/lib/registry.js index 762af385421..c9d178ac57d 100644 --- a/packages/container/lib/registry.js +++ b/packages/container/lib/registry.js @@ -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 }); } } @@ -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})); } diff --git a/packages/container/tests/container_test.js b/packages/container/tests/container_test.js index 23b0d69b007..76be958964d 100644 --- a/packages/container/tests/container_test.js +++ b/packages/container/tests/container_test.js @@ -400,7 +400,8 @@ moduleFor('Container', class extends AbstractTestCase { Apple.reopenClass({ _lazyInjections() { - return ['orange:main', 'banana:main']; + return [{ fullName: 'orange:main' }, + { fullName: 'banana:main' }]; } }); @@ -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' }]; } }); diff --git a/packages/ember-metal/lib/injected_property.js b/packages/ember-metal/lib/injected_property.js index 0a0e545bebc..2c6a4a6505b 100644 --- a/packages/ember-metal/lib/injected_property.js +++ b/packages/ember-metal/lib/injected_property.js @@ -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); @@ -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); } diff --git a/packages/ember-runtime/lib/inject.js b/packages/ember-runtime/lib/inject.js index 1360e5f6263..58372840491 100644 --- a/packages/ember-runtime/lib/inject.js +++ b/packages/ember-runtime/lib/inject.js @@ -35,7 +35,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); } /** diff --git a/packages/ember-runtime/lib/system/core_object.js b/packages/ember-runtime/lib/system/core_object.js index 43f9452f540..515b70bd025 100644 --- a/packages/ember-runtime/lib/system/core_object.js +++ b/packages/ember-runtime/lib/system/core_object.js @@ -1057,7 +1057,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 + }; } } diff --git a/packages/ember-runtime/tests/inject_test.js b/packages/ember-runtime/tests/inject_test.js index fe3f68397bd..d2e92b3b558 100644 --- a/packages/ember-runtime/tests/inject_test.js +++ b/packages/ember-runtime/tests/inject_test.js @@ -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'); }); } diff --git a/packages/ember/tests/service_injection_test.js b/packages/ember/tests/service_injection_test.js index e36e8cee009..a6e6ba8093f 100644 --- a/packages/ember/tests/service_injection_test.js +++ b/packages/ember/tests/service_injection_test.js @@ -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); + }); + } }); }