Skip to content

Commit

Permalink
Merge pull request #12629 from rwjblue/add-owner-injection
Browse files Browse the repository at this point in the history
[BUGFIX beta] Expose ownerInjection method on ContainerProxy.
  • Loading branch information
rwjblue committed Nov 20, 2015
2 parents f03eab4 + 6d063b8 commit cfed401
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
14 changes: 13 additions & 1 deletion packages/container/lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Ember from 'ember-metal/core';
import { assert, deprecate } from 'ember-metal/debug';
import dictionary from 'ember-metal/dictionary';
import isEnabled from 'ember-metal/features';
import { setOwner } from './owner';
import { setOwner, OWNER } from './owner';
import { buildFakeContainerWithDeprecations } from 'ember-runtime/mixins/container_proxy';

/**
Expand Down Expand Up @@ -159,6 +159,18 @@ Container.prototype = {
} else {
resetCache(this);
}
},

/**
Returns an object that can be used to provide an owner to a
manually created instance.
@private
@method ownerInjection
@returns { Object }
*/
ownerInjection() {
return { [OWNER]: this.owner };
}
};

Expand Down
12 changes: 11 additions & 1 deletion packages/container/tests/container_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ember from 'ember-metal/core';
import Registry from 'container/registry';
import factory from 'container/tests/test-helpers/factory';
import { getOwner } from 'container/owner';
import { getOwner, OWNER } from 'container/owner';
import isEnabled from 'ember-metal/features';

var originalModelInjections;
Expand Down Expand Up @@ -519,6 +519,16 @@ QUnit.test('Lazy injection validations are cached', function() {
container.lookup('apple:main');
});

QUnit.test('An object with its owner pre-set should be returned from ownerInjection', function() {
let owner = { };
var registry = new Registry();
var container = registry.container({ owner });

let result = container.ownerInjection();

equal(result[OWNER], owner, 'owner is properly included');
});

if (isEnabled('ember-container-inject-owner')) {
QUnit.test('A deprecated `container` property is appended to every object instantiated from an extendable factory', function() {
let registry = new Registry();
Expand Down
21 changes: 21 additions & 0 deletions packages/ember-runtime/lib/mixins/container_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ export default Mixin.create({
*/
__container__: null,

/**
Returns an object that can be used to provide an owner to a
manually created instance.
Example:
```
let owner = Ember.getOwner(this);
User.create(
owner.ownerInjection(),
{ username: 'rwjblue' }
)
```
@public
@method ownerInjection
@return {Object}
*/
ownerInjection: containerAlias('ownerInjection'),

/**
Given a fullName return a corresponding instance.
Expand Down
24 changes: 24 additions & 0 deletions packages/ember-runtime/tests/mixins/container_proxy_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { OWNER } from 'container/owner';
import Registry from 'container/registry';
import Container from 'container/container';
import ContainerProxy from 'ember-runtime/mixins/container_proxy';
import EmberObject from 'ember-runtime/system/object';

QUnit.module('ember-runtime/mixins/container_proxy', {
setup() {
this.Owner = EmberObject.extend(ContainerProxy);
this.instance = this.Owner.create();

let registry = new Registry();

this.instance.__container__ = new Container(registry, {
owner: this.instance
});
}
});

QUnit.test('provides ownerInjection helper method', function(assert) {
let result = this.instance.ownerInjection();

assert.equal(result[OWNER], this.instance, 'returns an object with the OWNER symbol');
});

0 comments on commit cfed401

Please sign in to comment.