diff --git a/packages/ember-application/lib/system/application-instance.js b/packages/ember-application/lib/system/application-instance.js index 8643941f341..f22e7522682 100644 --- a/packages/ember-application/lib/system/application-instance.js +++ b/packages/ember-application/lib/system/application-instance.js @@ -12,7 +12,7 @@ import EmberObject from 'ember-runtime/system/object'; import run from 'ember-metal/run_loop'; import { computed } from 'ember-metal/computed'; import Registry from 'container/registry'; -import RegistryProxy from 'ember-runtime/mixins/registry_proxy'; +import RegistryProxy, { buildFakeRegistryWithDeprecations } from 'ember-runtime/mixins/registry_proxy'; import ContainerProxy from 'ember-runtime/mixins/container_proxy'; import assign from 'ember-metal/assign'; @@ -99,7 +99,10 @@ let ApplicationInstance = EmberObject.extend(RegistryProxy, ContainerProxy, { // in tests, or rendered to a string in the case of FastBoot. this.register('-application-instance:main', this, { instantiate: false }); - assignAliases(this); + if (!isEnabled('ember-registry-container-reform')) { + this.container = this.__container__; + this.registry = this.__registry__; + } }, router: computed(function() { @@ -210,27 +213,30 @@ function isResolverModuleBased(applicationInstance) { return !!applicationInstance.application.__registry__.resolver.moduleBasedResolver; } -function assignAliases(applicationInstance) { - if (isEnabled('ember-registry-container-reform')) { - Object.defineProperty(applicationInstance, 'container', { - configurable: true, - enumerable: false, - get() { - var instance = this; - return { - lookup() { - Ember.deprecate('Using `ApplicationInstance.container.lookup` is deprecated. Please use `ApplicationInstance.lookup` instead.', - false, - { id: 'ember-application.app-instance-container', until: '3.0.0' }); - return instance.lookup(...arguments); - } - }; - } - }); - } else { - applicationInstance.container = applicationInstance.__container__; - applicationInstance.registry = applicationInstance.__registry__; - } +if (isEnabled('ember-registry-container-reform')) { + Object.defineProperty(ApplicationInstance.prototype, 'container', { + configurable: true, + enumerable: false, + get() { + var instance = this; + return { + lookup() { + Ember.deprecate('Using `ApplicationInstance.container.lookup` is deprecated. Please use `ApplicationInstance.lookup` instead.', + false, + { id: 'ember-application.app-instance-container', until: '3.0.0' }); + return instance.lookup(...arguments); + } + }; + } + }); + + Object.defineProperty(ApplicationInstance.prototype, 'registry', { + configurable: true, + enumerable: false, + get() { + return buildFakeRegistryWithDeprecations(this, 'ApplicationInstance'); + } + }); } export default ApplicationInstance; diff --git a/packages/ember-application/lib/system/application.js b/packages/ember-application/lib/system/application.js index c5e0f8e9430..7825f19193e 100644 --- a/packages/ember-application/lib/system/application.js +++ b/packages/ember-application/lib/system/application.js @@ -39,7 +39,7 @@ import LinkToComponent from 'ember-routing-views/views/link'; import RoutingService from 'ember-routing/services/routing'; import ContainerDebugAdapter from 'ember-extension-support/container_debug_adapter'; import { _loaded } from 'ember-runtime/system/lazy_load'; -import RegistryProxy from 'ember-runtime/mixins/registry_proxy'; +import RegistryProxy, { buildFakeRegistryWithDeprecations } from 'ember-runtime/mixins/registry_proxy'; import environment from 'ember-metal/environment'; function props(obj) { @@ -711,6 +711,16 @@ var Application = Namespace.extend(RegistryProxy, { } }); +if (isEnabled('ember-registry-container-reform')) { + Object.defineProperty(Application.prototype, 'registry', { + configurable: true, + enumerable: false, + get() { + return buildFakeRegistryWithDeprecations(this, 'Application'); + } + }); +} + Application.reopenClass({ /** Instance initializers run after all initializers have run. Because diff --git a/packages/ember-application/tests/system/application_instance_test.js b/packages/ember-application/tests/system/application_instance_test.js index f7a2970af4c..293085e5b08 100644 --- a/packages/ember-application/tests/system/application_instance_test.js +++ b/packages/ember-application/tests/system/application_instance_test.js @@ -46,11 +46,11 @@ QUnit.test('properties (and aliases) are correctly assigned for accessing the co ok(appInstance.__registry__, '#__registry__ is accessible'); if (isEnabled('ember-registry-container-reform')) { - expect(6); + expect(9); ok(typeof appInstance.container.lookup === 'function', '#container.lookup is available as a function'); - // stub `lookup` with a no-op to keep deprecation test simple + // stub with a no-op to keep deprecation test simple appInstance.__container__.lookup = function() { ok(true, '#loookup alias is called correctly'); }; @@ -58,6 +58,16 @@ QUnit.test('properties (and aliases) are correctly assigned for accessing the co expectDeprecation(function() { appInstance.container.lookup(); }, /Using `ApplicationInstance.container.lookup` is deprecated. Please use `ApplicationInstance.lookup` instead./); + + + ok(typeof appInstance.registry.register === 'function', '#registry.register is available as a function'); + appInstance.__registry__.register = function() { + ok(true, '#register alias is called correctly'); + }; + + expectDeprecation(function() { + appInstance.registry.register(); + }, /Using `ApplicationInstance.registry.register` is deprecated. Please use `ApplicationInstance.register` instead./); } else { expect(5); diff --git a/packages/ember-application/tests/system/application_test.js b/packages/ember-application/tests/system/application_test.js index fbe44e2383a..df4031fe5cb 100644 --- a/packages/ember-application/tests/system/application_test.js +++ b/packages/ember-application/tests/system/application_test.js @@ -13,6 +13,7 @@ import EmberRoute from 'ember-routing/system/route'; import jQuery from 'ember-views/system/jquery'; import compile from 'ember-template-compiler/system/compile'; import { _loaded } from 'ember-runtime/system/lazy_load'; +import isEnabled from 'ember-metal/features'; var trim = jQuery.trim; @@ -98,6 +99,22 @@ QUnit.test('acts like a namespace', function() { equal(app.Foo.toString(), 'TestApp.Foo', 'Classes pick up their parent namespace'); }); +if (isEnabled('ember-registry-container-reform')) { + QUnit.test('includes deprecated access to `application.registry`', function() { + expect(3); + + ok(typeof application.registry.register === 'function', '#registry.register is available as a function'); + + application.__registry__.register = function() { + ok(true, '#register alias is called correctly'); + }; + + expectDeprecation(function() { + application.registry.register(); + }, /Using `Application.registry.register` is deprecated. Please use `Application.register` instead./); + }); +} + QUnit.module('Ember.Application initialization', { teardown() { if (app) { diff --git a/packages/ember-runtime/lib/mixins/registry_proxy.js b/packages/ember-runtime/lib/mixins/registry_proxy.js index 429bad996d3..df6b0336513 100644 --- a/packages/ember-runtime/lib/mixins/registry_proxy.js +++ b/packages/ember-runtime/lib/mixins/registry_proxy.js @@ -1,3 +1,4 @@ +import Ember from 'ember-metal/core'; import { Mixin } from 'ember-metal/mixin'; export default Mixin.create({ @@ -246,3 +247,34 @@ function registryAlias(name) { return this.__registry__[name](...arguments); }; } + +export function buildFakeRegistryWithDeprecations(instance, typeForMessage) { + var fakeRegistry = {}; + var registryProps = { + resolve: 'resolveRegistration', + register: 'register', + unregister: 'unregister', + has: 'hasRegistration', + option: 'registerOption', + options: 'registerOptions', + getOptions: 'registeredOptions', + optionsForType: 'registerOptionsForType', + getOptionsForType: 'registeredOptionsForType', + injection: 'inject' + }; + + for (var deprecatedProperty in registryProps) { + fakeRegistry[deprecatedProperty] = buildFakeRegistryFunction(instance, typeForMessage, deprecatedProperty, registryProps[deprecatedProperty]); + } + + return fakeRegistry; +} + +function buildFakeRegistryFunction(instance, typeForMessage, deprecatedProperty, nonDeprecatedProperty) { + return function() { + Ember.deprecate(`Using \`${typeForMessage}.registry.${deprecatedProperty}\` is deprecated. Please use \`${typeForMessage}.${nonDeprecatedProperty}\` instead.`, + false, + { id: 'ember-application.app-instance-registry', until: '3.0.0' }); + return instance[nonDeprecatedProperty](...arguments); + }; +}