Skip to content
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

[BUGFIX beta] Allow deprecated access to registry. #12157

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions packages/ember-application/lib/system/application-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
12 changes: 11 additions & 1 deletion packages/ember-application/lib/system/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,28 @@ 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');
};

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);

Expand Down
17 changes: 17 additions & 0 deletions packages/ember-application/tests/system/application_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
32 changes: 32 additions & 0 deletions packages/ember-runtime/lib/mixins/registry_proxy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Ember from 'ember-metal/core';
import { Mixin } from 'ember-metal/mixin';

export default Mixin.create({
Expand Down Expand Up @@ -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);
};
}