Skip to content

Commit 7058914

Browse files
committed
Merge pull request ecomfe#30 from ecomfe/develop
release 0.3.0
2 parents 56cc036 + 436494e commit 7058914

28 files changed

+1558
-492
lines changed

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,61 @@ var ioc = IoC({ components: components });
256256

257257
如上代码:appData 与 creativeData 重用了 requestStrategy 的配置,并覆盖了 args 的配置项。
258258

259+
## 集合操作符 $list 与 $map
260+
261+
$list 与 $map 让我们可以声明某个依赖为数组或映射表集合,在集合配置中,我们还可以**嵌套的使用 uioc 提供的各种操作符($import, $ref, $list, $map)进行更深层次的依赖声明**
262+
263+
$list 可以让一系列的依赖以数组集合的形式注入到实例上,$list的配置为数组:{$list: ['value', {$ref: 'dep'}, {$list: ['nested value']}]}
264+
265+
$map 则是让一系列的依赖以对象集合的形式注入到实例上,$map的配置为简单的对象: {$map: {value: 1, dep: {$ref: 'dep'}, nest: {$map: {nestProp: 'nested prop'}}}
266+
267+
demo 如下:
268+
269+
```javascript
270+
var components = {
271+
a: {
272+
creator: function () {
273+
this.say = function () {
274+
console.log('a');
275+
};
276+
}
277+
},
278+
b: {
279+
creator: function () {
280+
this.say = function () {
281+
console.log('b');
282+
};
283+
}
284+
},
285+
main: {
286+
creator: function (name, listArg) {
287+
this.name = name;
288+
this.listArg = listArg;
289+
this.say = function () {
290+
console.log(this.name);
291+
this.listArg.forEach(function (item) {
292+
item.say();
293+
});
294+
};
295+
},
296+
args: ['main', {$list: [{$ref: 'a'}, {$ref: 'b'}]}],
297+
properties: {
298+
mapCollection: {
299+
a: {$ref: 'a'},
300+
b: {$ref: 'b'}
301+
}
302+
}
303+
}
304+
};
305+
306+
var ioc = IoC({components: components});
307+
ioc.getComponent('main', function (main) {
308+
main.say(); // 输出:main a b
309+
main.mapCollection.a.say(); // 输出 a
310+
main.mapCollection.b.say(); // 输出 b
311+
});
312+
313+
```
259314

260315
## [API](http://ecomfe.github.io/uioc/doc/IoC.html)
261316

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uioc",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "an ioc framework",
55
"main": "main.js",
66
"maintainers": {

src/Container.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/DependencyParser.js

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/DependencyTree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ void function (define) {
4444
return DependencyNode;
4545
});
4646

47-
}(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory; });
47+
}(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); });

src/Injector.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
void function (define) {
2+
define(
3+
function (require) {
4+
var u = require('./util');
5+
6+
function Injector(context) {
7+
this.context = context;
8+
this.singletons = {};
9+
}
10+
11+
Injector.prototype.createInstance = function (component, cb) {
12+
if (!component) {
13+
return cb(null);
14+
}
15+
16+
var id = component.id;
17+
if (component.scope === 'singleton' && u.hasOwn(this.singletons, id)) {
18+
return cb(this.singletons[id]);
19+
}
20+
21+
if (component.scope === 'static') {
22+
return cb(component.creator);
23+
}
24+
25+
var me = this;
26+
this.injectArgs(component, function (args) {
27+
var instance = component.creator.apply(null, args);
28+
if (component.scope === 'singleton') {
29+
me.singletons[id] = instance;
30+
}
31+
cb(instance);
32+
});
33+
};
34+
35+
Injector.prototype.injectArgs = function (componentConfig, cb) {
36+
var argConfigs = componentConfig.args;
37+
var count = argConfigs.length;
38+
var args = new Array(count);
39+
var ref = this.context.operators.ref;
40+
if (!count) {
41+
return cb(args);
42+
}
43+
44+
var done = function (index) {
45+
return function (instance) {
46+
args[index] = instance;
47+
--count === 0 && cb(args);
48+
};
49+
};
50+
51+
for (var i = argConfigs.length - 1; i > -1; --i) {
52+
var arg = argConfigs[i];
53+
ref.has(arg) ? this.context.getComponent(arg.$ref, done(i)) : done(i)(arg);
54+
}
55+
};
56+
57+
Injector.prototype.injectProperties = function injectProperties(instance, componentConfig, cb) {
58+
var deps = componentConfig.propDeps;
59+
var props = componentConfig.properties;
60+
var ref = this.context.operators.ref;
61+
var setter = this.context.operators.setter;
62+
63+
this.context.getComponent(deps, function () {
64+
for (var k in props) {
65+
var property = props[k];
66+
var value = ref.has(property) ? arguments[u.indexOf(deps, property.$ref)] : property;
67+
setter.setProperty(instance, k, value, setter.has(property) && property.$setter);
68+
}
69+
cb();
70+
});
71+
};
72+
73+
Injector.prototype.injectSetters = function (instance, componentConfig, cb) {
74+
var deps = componentConfig.setterDeps || [];
75+
var setter = this.context.operators.setter;
76+
77+
this.context.getComponent(deps, function () {
78+
for (var i = deps.length - 1; i > -1; --i) {
79+
var dep = deps[i];
80+
setter.setProperty(instance, dep, arguments[i]);
81+
}
82+
cb();
83+
});
84+
};
85+
86+
Injector.prototype.injectDependencies = function (instance, componentConfig, cb) {
87+
var complete = {
88+
prop: false,
89+
setter: false
90+
};
91+
var done = function (type) {
92+
complete[type] = true;
93+
complete.prop && complete.setter && cb();
94+
};
95+
this.injectProperties(instance, componentConfig, u.bind(done, null, 'prop'));
96+
this.injectSetters(instance, componentConfig, u.bind(done, null, 'setter'));
97+
};
98+
99+
Injector.prototype.dispose = function () {
100+
var singletons = this.singletons;
101+
for (var k in singletons) {
102+
var instance = singletons[k];
103+
instance && typeof instance.dispose === 'function' && instance.dispose();
104+
}
105+
106+
this.singletons = null;
107+
};
108+
109+
return Injector;
110+
});
111+
112+
}(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); });

0 commit comments

Comments
 (0)