Skip to content

Commit e09c61a

Browse files
committed
Merge pull request #11882 from dgeb/fix-11879
[BUGFIX] Correctly alias properties on Container and ApplicationInstance
2 parents 2a32a95 + f9f083b commit e09c61a

File tree

4 files changed

+102
-33
lines changed

4 files changed

+102
-33
lines changed

packages/container/lib/container.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ function resetMember(container, fullName) {
335335
// Once registry / container reform is enabled, we no longer need to expose
336336
// Container#_registry, since Container itself will be fully private.
337337
if (!isEnabled('ember-registry-container-reform')) {
338-
Object.defineProperty(Container, '_registry', {
338+
Object.defineProperty(Container.prototype, '_registry', {
339339
configurable: true,
340340
enumerable: false,
341341
get() {

packages/container/tests/container_test.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Ember from 'ember-metal/core';
22
import Registry from 'container/registry';
33
import { factory } from 'container/tests/container_helper';
4+
import isEnabled from 'ember-metal/features';
45

56
var originalModelInjections;
67

@@ -519,3 +520,13 @@ QUnit.test('Lazy injection validations are cached', function() {
519520
container.lookup('apple:main');
520521
container.lookup('apple:main');
521522
});
523+
524+
if (!isEnabled('ember-registry-container-reform')) {
525+
QUnit.test('Container#_registry provides an alias to Container#registry while Container is pseudo-public', function() {
526+
var registry = new Registry();
527+
var container = registry.container();
528+
529+
strictEqual(container.registry, registry, '#registry points to the parent registry');
530+
strictEqual(container._registry, registry, '#_registry is an alias to #registry');
531+
});
532+
}

packages/ember-application/lib/system/application-instance.js

+23-32
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ let ApplicationInstance = EmberObject.extend(RegistryProxy, ContainerProxy, {
9898
// appended to the rootElement, in the case of apps, to the fixture harness
9999
// in tests, or rendered to a string in the case of FastBoot.
100100
this.register('-application-instance:main', this, { instantiate: false });
101+
102+
assignAliases(this);
101103
},
102104

103105
router: computed(function() {
@@ -204,38 +206,27 @@ function isResolverModuleBased(applicationInstance) {
204206
return !!applicationInstance.application.__registry__.resolver.moduleBasedResolver;
205207
}
206208

207-
if (isEnabled('ember-registry-container-reform')) {
208-
Object.defineProperty(ApplicationInstance, 'container', {
209-
configurable: true,
210-
enumerable: false,
211-
get() {
212-
var instance = this;
213-
return {
214-
lookup() {
215-
Ember.deprecate('Using `ApplicationInstance.container.lookup` is deprecated. Please use `ApplicationInstance.lookup` instead.',
216-
false,
217-
{ id: 'ember-application.app-instance-container', until: '3.0.0' });
218-
return instance.lookup(...arguments);
219-
}
220-
};
221-
}
222-
});
223-
} else {
224-
Object.defineProperty(ApplicationInstance, 'container', {
225-
configurable: true,
226-
enumerable: false,
227-
get() {
228-
return this.__container__;
229-
}
230-
});
231-
232-
Object.defineProperty(ApplicationInstance, 'registry', {
233-
configurable: true,
234-
enumerable: false,
235-
get() {
236-
return this.__registry__;
237-
}
238-
});
209+
function assignAliases(applicationInstance) {
210+
if (isEnabled('ember-registry-container-reform')) {
211+
Object.defineProperty(applicationInstance, 'container', {
212+
configurable: true,
213+
enumerable: false,
214+
get() {
215+
var instance = this;
216+
return {
217+
lookup() {
218+
Ember.deprecate('Using `ApplicationInstance.container.lookup` is deprecated. Please use `ApplicationInstance.lookup` instead.',
219+
false,
220+
{ id: 'ember-application.app-instance-container', until: '3.0.0' });
221+
return instance.lookup(...arguments);
222+
}
223+
};
224+
}
225+
});
226+
} else {
227+
applicationInstance.container = applicationInstance.__container__;
228+
applicationInstance.registry = applicationInstance.__registry__;
229+
}
239230
}
240231

241232
export default ApplicationInstance;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Application from 'ember-application/system/application';
2+
import ApplicationInstance from 'ember-application/system/application-instance';
3+
import run from 'ember-metal/run_loop';
4+
import jQuery from 'ember-views/system/jquery';
5+
import isEnabled from 'ember-metal/features';
6+
7+
let app, appInstance;
8+
9+
QUnit.module('Ember.ApplicationInstance', {
10+
setup() {
11+
jQuery('#qunit-fixture').html('<div id=\'one\'><div id=\'one-child\'>HI</div></div><div id=\'two\'>HI</div>');
12+
run(function() {
13+
app = Application.create({ rootElement: '#one', router: null });
14+
});
15+
},
16+
17+
teardown() {
18+
jQuery('#qunit-fixture').empty();
19+
20+
if (appInstance) {
21+
run(appInstance, 'destroy');
22+
}
23+
24+
if (app) {
25+
run(app, 'destroy');
26+
}
27+
}
28+
});
29+
30+
QUnit.test('an application instance can be created based upon an application', function() {
31+
run(function() {
32+
appInstance = ApplicationInstance.create({ application: app });
33+
});
34+
35+
ok(appInstance, 'instance should be created');
36+
equal(appInstance.application, app, 'application should be set to parent');
37+
});
38+
39+
QUnit.test('properties (and aliases) are correctly assigned for accessing the container and registry', function() {
40+
run(function() {
41+
appInstance = ApplicationInstance.create({ application: app });
42+
});
43+
44+
ok(appInstance, 'instance should be created');
45+
ok(appInstance.__container__, '#__container__ is accessible');
46+
ok(appInstance.__registry__, '#__registry__ is accessible');
47+
48+
if (isEnabled('ember-registry-container-reform')) {
49+
expect(6);
50+
51+
ok(typeof appInstance.container.lookup === 'function', '#container.lookup is available as a function');
52+
53+
// stub `lookup` with a no-op to keep deprecation test simple
54+
appInstance.__container__.lookup = function() {
55+
ok(true, '#loookup alias is called correctly');
56+
};
57+
58+
expectDeprecation(function() {
59+
appInstance.container.lookup();
60+
}, /Using `ApplicationInstance.container.lookup` is deprecated. Please use `ApplicationInstance.lookup` instead./);
61+
} else {
62+
expect(5);
63+
64+
strictEqual(appInstance.container, appInstance.__container__, '#container alias should be assigned');
65+
strictEqual(appInstance.registry, appInstance.__registry__, '#registry alias should be assigned');
66+
}
67+
});

0 commit comments

Comments
 (0)