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

[CLEANUP beta] Remove deprecated Registry and Container behavior #11853

Merged
merged 2 commits into from
Jul 21, 2015
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
47 changes: 3 additions & 44 deletions packages/container/lib/container.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import Ember from 'ember-metal/core'; // Ember.assert
import dictionary from 'ember-metal/dictionary';

// TODO - Temporary workaround for v0.4.0 of the ES6 transpiler, which lacks support for circular dependencies.
// See the below usage of requireModule. Instead, it should be possible to simply `import Registry from './registry';`
var Registry;

/**
A container used to instantiate and cache objects.

Expand All @@ -19,24 +15,15 @@ var Registry;
@class Container
*/
function Container(registry, options) {
this._registry = registry || (function() {
Ember.deprecate('A container should only be created for an already instantiated registry. For backward compatibility, an isolated registry will be instantiated just for this container.', false, { id: 'container.instantiate-without-registry', until: '3.0.0' });

// TODO - See note above about transpiler import workaround.
if (!Registry) { Registry = requireModule('container/registry')['default']; }

return new Registry();
}());

this.cache = dictionary(options && options.cache ? options.cache : null);
this.factoryCache = dictionary(options && options.factoryCache ? options.factoryCache : null);
this._registry = registry;
this.cache = dictionary(options && options.cache ? options.cache : null);
this.factoryCache = dictionary(options && options.factoryCache ? options.factoryCache : null);
this.validationCache = dictionary(options && options.validationCache ? options.validationCache : null);
}

Container.prototype = {
/**
@private

@property _registry
@type Registry
@since 1.11.0
Expand All @@ -45,7 +32,6 @@ Container.prototype = {

/**
@private

@property cache
@type InheritingDict
*/
Expand Down Expand Up @@ -160,33 +146,6 @@ Container.prototype = {
}
};

(function exposeRegistryMethods() {
var methods = [
'register',
'unregister',
'resolve',
'normalize',
'typeInjection',
'injection',
'factoryInjection',
'factoryTypeInjection',
'has',
'options',
'optionsForType'
];

function exposeRegistryMethod(method) {
Container.prototype[method] = function() {
Ember.deprecate(method + ' should be called on the registry instead of the container', false, { id: 'container.deprecated-registry-method-on-container', until: '3.0.0' });
return this._registry[method].apply(this._registry, arguments);
};
}

for (var i = 0, l = methods.length; i < l; i++) {
exposeRegistryMethod(methods[i]);
}
})();

function lookup(container, fullName, options) {
options = options || {};

Expand Down
70 changes: 1 addition & 69 deletions packages/container/lib/registry.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import Ember from 'ember-metal/core'; // Ember.assert
import isEnabled from 'ember-metal/features';
import dictionary from 'ember-metal/dictionary';
import { assign } from 'ember-metal/merge';
import Container from './container';

var VALID_FULL_NAME_REGEXP = /^[^:]+.+:[^:]+$/;

var instanceInitializersFeatureEnabled;
if (isEnabled('ember-application-instance-initializers')) {
instanceInitializersFeatureEnabled = true;
}

/**
A registry used to store factory and option information keyed
by type.
Expand Down Expand Up @@ -133,18 +127,6 @@ Registry.prototype = {
*/
_typeOptions: null,

/**
The first container created for this registry.

This allows deprecated access to `lookup` and `lookupFactory` to avoid
breaking compatibility for Ember 1.x initializers.

@private
@property _defaultContainer
@type Container
*/
_defaultContainer: null,

/**
Creates a container based on this registry.

Expand All @@ -154,52 +136,7 @@ Registry.prototype = {
@return {Container} created container
*/
container(options) {
var container = new Container(this, options);

// 2.0TODO - remove `registerContainer`
this.registerContainer(container);

return container;
},

/**
Register the first container created for a registery to allow deprecated
access to its `lookup` and `lookupFactory` methods to avoid breaking
compatibility for Ember 1.x initializers.

2.0TODO: Remove this method. The bookkeeping is only needed to support
deprecated behavior.

@private
@param {Container} newly created container
*/
registerContainer(container) {
if (!this._defaultContainer) {
this._defaultContainer = container;
}
if (this.fallback) {
this.fallback.registerContainer(container);
}
},

lookup(fullName, options) {
Ember.assert('Create a container on the registry (with `registry.container()`) before calling `lookup`.', this._defaultContainer);

if (instanceInitializersFeatureEnabled) {
Ember.deprecate('`lookup` was called on a Registry. The `initializer` API no longer receives a container, and you should use an `instanceInitializer` to look up objects from the container.', false, { id: 'container.deprecate-lookup-access-to-instances-in-initializers', until: '3.0.0', url: 'http://emberjs.com/guides/deprecations#toc_deprecate-access-to-instances-in-initializers' });
}

return this._defaultContainer.lookup(fullName, options);
},

lookupFactory(fullName) {
Ember.assert('Create a container on the registry (with `registry.container()`) before calling `lookupFactory`.', this._defaultContainer);

if (instanceInitializersFeatureEnabled) {
Ember.deprecate('`lookupFactory` was called on a Registry. The `initializer` API no longer receives a container, and you should use an `instanceInitializer` to look up objects from the container.', false, { id: 'container.deprecate-lookupfactory-access-to-instances-in-initializers', until: '3.0.0', url: 'http://emberjs.com/guides/deprecations#toc_deprecate-access-to-instances-in-initializers' });
}

return this._defaultContainer.lookupFactory(fullName);
return new Container(this, options);
},

/**
Expand Down Expand Up @@ -457,11 +394,6 @@ Registry.prototype = {
}
},

option(fullName, optionName) {
Ember.deprecate('`Registry.option()` has been deprecated. Call `Registry.getOption()` instead.', false, { id: 'container.deprecated-registry-option', until: '3.0.0' });
return this.getOption(fullName, optionName);
},

/**
Used only via `injection`.

Expand Down
3 changes: 1 addition & 2 deletions packages/container/tests/registry_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ QUnit.test('factory for non extendables resolves are cached', function() {
deepEqual(resolveWasCalled, ['foo:post']);
});

QUnit.test('registry.container creates an associated container', function() {
QUnit.test('registry.container creates a container', function() {
var registry = new Registry();
var PostController = factory();
registry.register('controller:post', PostController);
Expand All @@ -269,7 +269,6 @@ QUnit.test('registry.container creates an associated container', function() {
var postController = container.lookup('controller:post');

ok(postController instanceof PostController, 'The lookup is an instance of the registered factory');
strictEqual(registry._defaultContainer, container, '_defaultContainer is set to the first created container and used for Ember 1.x Container compatibility');
});

QUnit.test('`resolve` can be handled by a fallback registry', function() {
Expand Down
31 changes: 13 additions & 18 deletions packages/ember-application/lib/system/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,15 @@ var Application = Namespace.extend({
run.join(this, handleReset);
},


/**
@private
@method instanceInitializer
*/
instanceInitializer(options) {
this.constructor.instanceInitializer(options);
},

/**
@private
@method runInitializers
Expand All @@ -667,13 +676,7 @@ var Application = Namespace.extend({
var App = this;
this._runInitializer('initializers', function(name, initializer) {
Ember.assert('No application initializer named \'' + name + '\'', !!initializer);

if (isEnabled('ember-application-initializer-context')) {
initializer.initialize(registry, App);
} else {
var ref = initializer.initialize;
ref(registry, App);
}
initializer.initialize(registry, App);
});
},

Expand Down Expand Up @@ -772,17 +775,9 @@ var Application = Namespace.extend({
}
});

if (isEnabled('ember-application-instance-initializers')) {
Application.reopen({
instanceInitializer(options) {
this.constructor.instanceInitializer(options);
}
});

Application.reopenClass({
instanceInitializer: buildInitializerMethod('instanceInitializers', 'instance initializer')
});
}
Application.reopenClass({
instanceInitializer: buildInitializerMethod('instanceInitializers', 'instance initializer')
});

if (isEnabled('ember-application-visit')) {
Application.reopen({
Expand Down
77 changes: 14 additions & 63 deletions packages/ember-application/tests/system/initializers_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Ember from 'ember-metal/core';
import isEnabled from 'ember-metal/features';
import run from 'ember-metal/run_loop';
import Application from 'ember-application/system/application';
import jQuery from 'ember-views/system/jquery';
Expand Down Expand Up @@ -333,70 +332,22 @@ QUnit.test('initializers are per-app', function() {
});
});

if (isEnabled('ember-application-initializer-context')) {
QUnit.test('initializers should be executed in their own context', function() {
expect(1);
var MyApplication = Application.extend();

MyApplication.initializer({
name: 'coolBabeInitializer',
myProperty: 'coolBabe',
initialize(registry, application) {
equal(this.myProperty, 'coolBabe', 'should have access to its own context');
}
});

run(function() {
app = MyApplication.create({
router: false,
rootElement: '#qunit-fixture'
});
});
});
}

if (isEnabled('ember-application-instance-initializers')) {
QUnit.test('initializers should throw a deprecation warning when performing a lookup on the registry', function() {
expect(1);

var MyApplication = Application.extend();

MyApplication.initializer({
name: 'initializer',
initialize(registry, application) {
registry.lookup('router:main');
}
});
QUnit.test('initializers should be executed in their own context', function() {
expect(1);
var MyApplication = Application.extend();

expectDeprecation(function() {
run(function() {
app = MyApplication.create({
router: false,
rootElement: '#qunit-fixture'
});
});
}, /`lookup` was called on a Registry\. The `initializer` API no longer receives a container, and you should use an `instanceInitializer` to look up objects from the container\./);
MyApplication.initializer({
name: 'coolInitializer',
myProperty: 'cool',
initialize(registry, application) {
equal(this.myProperty, 'cool', 'should have access to its own context');
}
});

QUnit.test('initializers should throw a deprecation warning when performing a factory lookup on the registry', function() {
expect(1);

var MyApplication = Application.extend();

MyApplication.initializer({
name: 'initializer',
initialize(registry, application) {
registry.lookupFactory('application:controller');
}
run(function() {
app = MyApplication.create({
router: false,
rootElement: '#qunit-fixture'
});

expectDeprecation(function() {
run(function() {
app = MyApplication.create({
router: false,
rootElement: '#qunit-fixture'
});
});
}, /`lookupFactory` was called on a Registry\. The `initializer` API no longer receives a container, and you should use an `instanceInitializer` to look up objects from the container\./);
});
}
});
Loading