Skip to content

Commit bb23658

Browse files
committed
Merge pull request ecomfe#25 from ecomfe/develop
release 0.2.0
2 parents df4e31d + cbadef8 commit bb23658

File tree

7 files changed

+98
-51
lines changed

7 files changed

+98
-51
lines changed

src/DependencyParser.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ void function (define) {
4747
for (var k in instance) {
4848
if (typeof instance[k] === 'function') {
4949
prop = this.getPropertyFromSetter(k);
50-
prop && !exclude.hasOwnProperty(prop) && deps.push(prop);
50+
51+
// 有属性,未排除,已注册
52+
prop && !exclude.hasOwnProperty(prop) && this.context.hasComponent(prop) && deps.push(prop);
5153
}
5254
}
5355
return deps;

src/main.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void function (define, global, undefined) {
3333
}
3434

3535
this.moduleLoader = config.loader || globalLoader;
36-
this.parser = new (config.parser || Parser)(this);
36+
this.parser = new Parser(this);
3737
this.components = {};
3838
this.container = new Container(this);
3939
this.addComponent(config.components || {});
@@ -161,6 +161,10 @@ void function (define, global, undefined) {
161161
return this;
162162
};
163163

164+
IoC.prototype.hasComponent = function (id) {
165+
return !!this.components[id];
166+
};
167+
164168
IoC.prototype.getComponentConfig = function (id) {
165169
return this.components[id];
166170
};
@@ -260,7 +264,7 @@ void function (define, global, undefined) {
260264
if (u.hasImport(args[i])) {
261265
// 给匿名组件配置生成一个 ioc 构件id
262266
id = createAnonymousComponent(context, component, args[i], '$arg.');
263-
args[i] = { $ref: id };
267+
args[i] = {$ref: id};
264268
component.anonyDeps.push(id);
265269
}
266270
}
@@ -269,7 +273,7 @@ void function (define, global, undefined) {
269273
for (var k in props) {
270274
if (u.hasImport(props[k])) {
271275
id = createAnonymousComponent(context, component, props[k], '$prop.');
272-
props[k] = { $ref: id };
276+
props[k] = {$ref: id};
273277
component.anonyDeps.push(id);
274278
}
275279
}

src/util.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void function (define, undefined) {
3333

3434
function warn() {
3535
if (typeof console !== 'undefined') {
36-
console.warn.apply(console, arguments);
36+
Function.prototype.apply.call(console.warn, console, arguments);
3737
}
3838
}
3939

@@ -58,7 +58,7 @@ void function (define, undefined) {
5858
return function () {
5959
var scope = args.shift();
6060
args.push.apply(args, arguments);
61-
fn.apply(scope, args);
61+
Function.prototype.apply.call(fn, scope, args);
6262
};
6363
}
6464

@@ -101,4 +101,4 @@ void function (define, undefined) {
101101
};
102102
});
103103

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

test/assets/AutoInject.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ define(
3535

3636
};
3737

38+
AutoInject.prototype.setUnRegisterComponent = function () {
39+
40+
};
41+
3842
return AutoInject;
3943
}
4044
);

test/spec/auto.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
describe('auto inject test', function () {
2+
3+
function assertInstanceOf(Constructor, instance) {
4+
expect(instance instanceof Constructor).toBe(true);
5+
}
6+
7+
function assertSame(a, b) {
8+
expect(a).toBe(b);
9+
}
10+
11+
function assertEqual(a, b) {
12+
expect(a).toEqual(b);
13+
}
14+
15+
function assertNull(v) {
16+
expect(v).toBeNull();
17+
}
18+
19+
var iocInstance = null;
20+
beforeEach(function (done) {
21+
require(['ioc', 'config'], function (IoC, config) {
22+
iocInstance = IoC(config());
23+
done();
24+
});
25+
});
26+
27+
it('normal inject', function (done) {
28+
require(['A', 'B', 'C', 'D', 'MyUtil', 'AutoInject', 'AutoInject1'],
29+
function (A, B, C, D, Util, AutoInject, AutoInject1) {
30+
spyOn(AutoInject.prototype, 'setd');
31+
spyOn(AutoInject.prototype, 'settest');
32+
iocInstance.getComponent('autoInject', function (autoInject) {
33+
34+
assertInstanceOf(A, autoInject.a);
35+
assertInstanceOf(B, autoInject.b);
36+
assertInstanceOf(C, autoInject.c);
37+
assertInstanceOf(D, autoInject.d);
38+
assertInstanceOf(Util, autoInject.d.b.util);
39+
40+
assertNull(autoInject.e);
41+
expect(autoInject.setd).not.toHaveBeenCalled();
42+
expect(autoInject.settest).not.toHaveBeenCalled();
43+
44+
var anotherInject = autoInject.anotherAutoInject;
45+
assertInstanceOf(AutoInject, anotherInject);
46+
assertInstanceOf(AutoInject1, anotherInject);
47+
assertInstanceOf(A, anotherInject.a);
48+
assertInstanceOf(B, anotherInject.b);
49+
assertInstanceOf(C, anotherInject.c);
50+
assertInstanceOf(D, anotherInject.d);
51+
assertInstanceOf(Util, anotherInject.d.b.util);
52+
53+
assertNull(anotherInject.e);
54+
done();
55+
});
56+
});
57+
});
58+
59+
it('setter and property priority', function (done) {
60+
iocInstance.getComponent('autoInject', function (autoInject) {
61+
require(['MyFactory'], function (MyFactory) {
62+
expect(autoInject.myFactory).toBe('myFactory');
63+
expect(autoInject.setCCalledCount).toBe(1);
64+
assertInstanceOf(MyFactory, autoInject.anotherAutoInject.myFactory);
65+
done();
66+
});
67+
});
68+
});
69+
70+
it('setter dependency which has no register', function (done) {
71+
require(['AutoInject'], function (AutoInject) {
72+
spyOn(AutoInject.prototype, 'setUnRegisterComponent');
73+
iocInstance.getComponent('autoInject', function (autoInject) {
74+
expect(autoInject.setUnRegisterComponent).not.toHaveBeenCalled();
75+
done();
76+
});
77+
});
78+
});
79+
80+
});

test/spec/integration.js

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -228,49 +228,6 @@ describe('Ioc Integration Test: ', function () {
228228
}).toThrow();
229229
});
230230

231-
it('autoInject', function (done) {
232-
iocInstance.getComponent('autoInject', function (autoInject) {
233-
require(['A', 'B', 'C', 'D', 'MyUtil', 'AutoInject', 'AutoInject1'],
234-
function (A, B, C, D, Util, AutoInject, AutoInject1) {
235-
spyOn(autoInject, 'setd');
236-
spyOn(autoInject, 'settest');
237-
assertInstanceOf(A, autoInject.a);
238-
assertInstanceOf(B, autoInject.b);
239-
assertInstanceOf(C, autoInject.c);
240-
assertInstanceOf(D, autoInject.d);
241-
assertInstanceOf(Util, autoInject.d.b.util);
242-
243-
assertNull(autoInject.e);
244-
expect(autoInject.setd).not.toHaveBeenCalled();
245-
expect(autoInject.settest).not.toHaveBeenCalled();
246-
247-
var anotherInject = autoInject.anotherAutoInject;
248-
assertInstanceOf(AutoInject, anotherInject);
249-
assertInstanceOf(AutoInject1, anotherInject);
250-
assertInstanceOf(A, anotherInject.a);
251-
assertInstanceOf(B, anotherInject.b);
252-
assertInstanceOf(C, anotherInject.c);
253-
assertInstanceOf(D, anotherInject.d);
254-
assertInstanceOf(Util, anotherInject.d.b.util);
255-
256-
assertNull(anotherInject.e);
257-
done();
258-
});
259-
});
260-
});
261-
262-
it('autoInject setter priority', function (done) {
263-
iocInstance.getComponent('autoInject', function (autoInject) {
264-
require(['MyFactory'], function (MyFactory) {
265-
expect(autoInject.myFactory).toBe('myFactory');
266-
expect(autoInject.setCCalledCount).toBe(1);
267-
assertInstanceOf(MyFactory, autoInject.anotherAutoInject.myFactory);
268-
done();
269-
});
270-
});
271-
});
272-
273-
274231
/* it('circularAllowed', 1, function (done) {
275232
276233
iocInstance.allowCircular = true;

test/test-main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require.config({
77
}
88
],
99
paths: {
10-
jquery: '//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery'
10+
jquery: 'https://ss0.bdstatic.com/5a21bjqh_Q23odCf/static/superplus/js/lib/jquery-1.10.2_d88366fd'
1111
}
1212
});
1313

0 commit comments

Comments
 (0)