Skip to content

Commit

Permalink
Merge pull request #12699 from rwjblue/do-not-change-frozen-things
Browse files Browse the repository at this point in the history
[BUGFIX beta] Only add deprecated container after create when present.
  • Loading branch information
rwjblue committed Dec 9, 2015
2 parents b3ce376 + b6e783d commit 31eadb4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/container/lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
45 changes: 44 additions & 1 deletion packages/container/tests/container_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.');

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 31eadb4

Please sign in to comment.