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] Only add deprecated container after create when present. #12699

Merged
merged 1 commit into from
Dec 9, 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
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