diff --git a/config/environment.js b/config/environment.js index a8496c9..1a31ea8 100644 --- a/config/environment.js +++ b/config/environment.js @@ -8,7 +8,8 @@ module.exports = function(/* environment, appConfig */) { _ENABLE_DEPRECATION_OPTIONS_SUPPORT: true, _ENABLE_WARN_OPTIONS_SUPPORT: true, _ENABLE_SAFE_STRING_SUPPORT: true, - _ENABLE_ENUMERABLE_CONTAINS_SUPPORT: true + _ENABLE_ENUMERABLE_CONTAINS_SUPPORT: true, + _ENABLE_DEPRECATED_REGISTRY_SUPPORT: true } }; }; diff --git a/index.js b/index.js index 891bbaa..c85f05c 100644 --- a/index.js +++ b/index.js @@ -10,5 +10,6 @@ module.exports = { this.import('vendor/ember-k.js'); this.import('vendor/safe-string.js'); this.import('vendor/enumerable-contains.js'); + this.import('vendor/deprecated-registry.js'); } }; diff --git a/tests/unit/deprecated-registry-test.js b/tests/unit/deprecated-registry-test.js new file mode 100644 index 0000000..eae8f2c --- /dev/null +++ b/tests/unit/deprecated-registry-test.js @@ -0,0 +1,13 @@ +import Ember from 'ember'; +import { module, test } from 'qunit'; + +module('Deprecated registry'); + +test('that Ember.Application.registry is correctly polyfilled', assert => { + assert.equal(typeof Ember.Application.prototype.registry, 'object', 'registry is present on Ember.Application'); +}); + +test('that Ember.ApplicationInstance.registry is correctly polyfilled', assert => { + assert.equal(typeof Ember.ApplicationInstance.prototype.registry, 'object', 'registry is present on Ember.ApplicationInstance'); +}); + diff --git a/vendor/deprecated-registry.js b/vendor/deprecated-registry.js new file mode 100644 index 0000000..0b9d50e --- /dev/null +++ b/vendor/deprecated-registry.js @@ -0,0 +1,69 @@ +(function () { + var _Ember; + if (typeof Ember !== 'undefined') { + _Ember = Ember; + } else { + _Ember = require('ember').default; + } + + if (EmberENV && EmberENV._ENABLE_DEPRECATED_REGISTRY_SUPPORT !== true) { + return false; + } + + if (!_Ember.Application.prototype.registry) { + Object.defineProperty(_Ember.Application.prototype, 'registry', { + configurable: true, + enumerable: false, + get() { + return buildFakeRegistryWithDeprecations(this, 'Application'); + } + }); + } + + if (!_Ember.ApplicationInstance.prototype.registry) { + Object.defineProperty(_Ember.ApplicationInstance.prototype, 'registry', { + configurable: true, + enumerable: false, + get() { + return buildFakeRegistryWithDeprecations(this, 'ApplicationInstance'); + } + }); + } + + function buildFakeRegistryWithDeprecations(instance, typeForMessage) { + let fakeRegistry = {}; + let registryProps = { + resolve: 'resolveRegistration', + register: 'register', + unregister: 'unregister', + has: 'hasRegistration', + option: 'registerOption', + options: 'registerOptions', + getOptions: 'registeredOptions', + optionsForType: 'registerOptionsForType', + getOptionsForType: 'registeredOptionsForType', + injection: 'inject' + }; + + for (let 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', + url: 'https://emberjs.com/deprecations/v2.x/#toc_ember-application-registry-ember-applicationinstance-registry' + } + ); + return instance[nonDeprecatedProperty](...arguments); + }; + } +})();