/
container_proxy.js
148 lines (116 loc) · 3.41 KB
/
container_proxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
@module ember
@submodule ember-runtime
*/
import run from 'ember-metal/run_loop';
import { deprecate } from 'ember-metal/debug';
import { Mixin } from 'ember-metal/mixin';
/**
ContainerProxyMixin is used to provide public access to specific
container functionality.
@class ContainerProxyMixin
@private
*/
export default Mixin.create({
/**
The container stores state.
@private
@property {Ember.Container} __container__
*/
__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.
The default behaviour is for lookup to return a singleton instance.
The singleton is scoped to the container, allowing multiple containers
to all have their own locally scoped singletons.
```javascript
var registry = new Registry();
var container = registry.container();
registry.register('api:twitter', Twitter);
var twitter = container.lookup('api:twitter');
twitter instanceof Twitter; // => true
// by default the container will return singletons
var twitter2 = container.lookup('api:twitter');
twitter2 instanceof Twitter; // => true
twitter === twitter2; //=> true
```
If singletons are not wanted an optional flag can be provided at lookup.
```javascript
var registry = new Registry();
var container = registry.container();
registry.register('api:twitter', Twitter);
var twitter = container.lookup('api:twitter', { singleton: false });
var twitter2 = container.lookup('api:twitter', { singleton: false });
twitter === twitter2; //=> false
```
@public
@method lookup
@param {String} fullName
@param {Object} options
@return {any}
*/
lookup: containerAlias('lookup'),
/**
Given a fullName return the corresponding factory.
@private
@method _lookupFactory
@param {String} fullName
@return {any}
*/
_lookupFactory: containerAlias('lookupFactory'),
/**
@private
*/
willDestroy() {
this._super(...arguments);
if (this.__container__) {
run(this.__container__, 'destroy');
}
}
});
function containerAlias(name) {
return function () {
return this.__container__[name](...arguments);
};
}
export function buildFakeContainerWithDeprecations(container) {
let fakeContainer = {};
let propertyMappings = {
lookup: 'lookup',
lookupFactory: '_lookupFactory'
};
for (let containerProperty in propertyMappings) {
fakeContainer[containerProperty] = buildFakeContainerFunction(container, containerProperty, propertyMappings[containerProperty]);
}
return fakeContainer;
}
function buildFakeContainerFunction(container, containerProperty, ownerProperty) {
return function() {
deprecate(
`Using the injected \`container\` is deprecated. Please use the \`getOwner\` helper to access the owner of this object and then call \`${ownerProperty}\` instead.`,
false,
{
id: 'ember-application.injected-container',
until: '3.0.0',
url: 'http://emberjs.com/deprecations/v2.x#toc_injected-container-access'
}
);
return container[containerProperty](...arguments);
};
}