diff --git a/packages/container/lib/container.js b/packages/container/lib/container.js index 1872ee49bc5..7e7cecc1abe 100644 --- a/packages/container/lib/container.js +++ b/packages/container/lib/container.js @@ -385,7 +385,9 @@ function instantiate(container, fullName) { // TODO - remove when Ember reaches v3.0.0 if (isEnabled('ember-container-inject-owner')) { - injectDeprecatedContainer(obj, container); + if (!Object.isFrozen(obj) && 'container' in obj) { + injectDeprecatedContainer(obj, container); + } } } diff --git a/packages/container/tests/container_test.js b/packages/container/tests/container_test.js index b5601465446..8049ca1df56 100644 --- a/packages/container/tests/container_test.js +++ b/packages/container/tests/container_test.js @@ -561,7 +561,10 @@ if (isEnabled('ember-container-inject-owner')) { let container = registry.container({ owner }); // Define a simple non-extendable factory - let PostController = function() {}; + let PostController = function(options) { + this.container = options.container; + }; + PostController.create = function(options) { ok(options.container, 'fake container has been injected and is available during `create`.'); @@ -599,6 +602,46 @@ if (isEnabled('ember-container-inject-owner')) { strictEqual(c, container, 'Injected container is now regular (not fake) container, but access is still deprecated.'); }, 'Using the injected `container` is deprecated. Please use the `getOwner` helper instead to access the owner of this object.'); }); + + QUnit.test('A deprecated `container` property is only set on a non-extendable factory instance if `container` is present and writable.', function() { + expect(2); + + let owner = {}; + let registry = new Registry(); + let container = registry.container({ owner }); + + // Define a non-extendable factory that is frozen after `create` + let PostController = function() {}; + PostController.create = function() { + let instance = new PostController(); + + Object.seal(instance); + + return instance; + }; + + registry.register('controller:post', PostController); + let postController = container.lookup('controller:post'); + + equal(postController.container, undefined, 'container was not added'); + + let OtherController = function() { + this.container = 'foo'; + }; + + OtherController.create = function() { + let instance = new OtherController(); + + Object.freeze(instance); + + return instance; + }; + + registry.register('controller:other', OtherController); + let otherController = container.lookup('controller:other'); + + equal(otherController.container, 'foo', 'container was not added'); + }); } else { QUnit.test('A `container` property is appended to every instantiated object', function() { let registry = new Registry();