Skip to content

Commit 51e5e6e

Browse files
committed
Refine the code
1 parent 51f4180 commit 51e5e6e

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

lib/app.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ export default class App {
164164
*/
165165
createObject(classname, data) {
166166
const ctor = this.getClass(classname);
167-
return Schema.instantiate(this, ctor, data || {});
167+
let obj = new ctor();
168+
Schema.parse(this, obj, data || {});
169+
170+
return obj;
168171
}
169172

170173
/**
@@ -437,8 +440,32 @@ export default class App {
437440
});
438441
}
439442

443+
_finalizeComp(comp, data, entities) {
444+
let ent = comp._entity;
445+
let proto = comp.constructor;
446+
447+
// add event listeners
448+
while (proto.__classname__ !== undefined) {
449+
if (proto.hasOwnProperty('events')) {
450+
for (let name in proto.events) {
451+
let method = proto.events[name];
452+
let fn = comp[method];
453+
if (fn) {
454+
fn = fn.bind(comp);
455+
ent.on(name, fn);
456+
comp.__events__.push({ name, fn });
457+
}
458+
}
459+
}
460+
proto = Object.getPrototypeOf(proto);
461+
}
462+
463+
// parse schema data
464+
Schema.parse(this, comp, data, entities);
465+
}
466+
440467
_createComp(ctor, ent, data = {}) {
441-
let comp = Schema.instantiate(this, ctor, data);
468+
let comp = new ctor();
442469
comp._app = this;
443470
comp._system = this._getSystem(comp);
444471
comp._entity = ent;
@@ -461,6 +488,9 @@ export default class App {
461488
proto = Object.getPrototypeOf(proto);
462489
}
463490

491+
// parse schema data
492+
Schema.parse(this, comp, data);
493+
464494
// invoke onInit & onEnable when entity is ready
465495
if (ent._ready) {
466496
// invoke onInit

lib/schema.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function _getClassParser(typename) {
2-
return function (app, data) {
2+
return function (app, data, propInfo, entities) {
33
let ctor = app.getClass(typename);
44
if (ctor === undefined) {
55
console.warn(`Can not find class ${typename}.`);
@@ -17,17 +17,20 @@ function _getClassParser(typename) {
1717
return null;
1818
}
1919

20-
return instantiate(app, ctor, data);
20+
let obj = new ctor();
21+
parse(app, obj, data, entities);
22+
23+
return obj;
2124
};
2225
}
2326

2427
function _getArrayParser(elementParser) {
2528
if (elementParser) {
26-
return function (app, data) {
29+
return function (app, data, propInfo, entities) {
2730
let result = new Array(data.length);
2831

2932
for (let i = 0; i < data.length; ++i) {
30-
result[i] = elementParser(app, data[i]);
33+
result[i] = elementParser(app, data[i], propInfo, entities);
3134
}
3235

3336
return result;
@@ -160,10 +163,9 @@ function createPrototypeAccessors(app, schema) {
160163
return prototypeAccessors;
161164
}
162165

163-
function instantiate(app, ctor, data) {
164-
let obj = new ctor();
166+
function parse(app, obj, data, entities) {
167+
let proto = obj.constructor;
165168

166-
let proto = ctor;
167169
while (proto.__classname__ !== undefined) {
168170
if (proto.hasOwnProperty('schema') === false) {
169171
proto = Object.getPrototypeOf(proto);
@@ -187,7 +189,7 @@ function instantiate(app, ctor, data) {
187189

188190
//
189191
if (parser) {
190-
let result = parser(app, value, propInfo);
192+
let result = parser(app, value, propInfo, entities);
191193
obj[interName] = result;
192194
} else {
193195
obj[interName] = value;
@@ -196,11 +198,9 @@ function instantiate(app, ctor, data) {
196198

197199
proto = Object.getPrototypeOf(proto);
198200
}
199-
200-
return obj;
201201
}
202202

203203
export default {
204204
createPrototypeAccessors,
205-
instantiate,
205+
parse,
206206
};

0 commit comments

Comments
 (0)