componentEventHandler serialize compId instead of compName (#2973)

This commit is contained in:
PP
2018-07-25 23:43:14 +08:00
committed by pandamicro
parent 751b36cf1e
commit 8daee8cd31
@@ -63,8 +63,18 @@ cc.Component.EventHandler = cc.Class({
* @type {String}
* @default ''
*/
component: {
default: '',
// only for deserializing old project component field
component: '',
_componentId: '',
_componentName: {
get () {
this._genCompIdIfNeeded();
return this._compId2Name(this._componentId);
},
set (value) {
this._componentId = this._compName2Id(value);
},
},
/**
* !#en Event handler
@@ -131,7 +141,10 @@ cc.Component.EventHandler = cc.Class({
var target = this.target;
if (!cc.isValid(target)) return;
var comp = target.getComponent(this.component);
this._genCompIdIfNeeded();
var compType = cc.js._getClassById(this._componentId);
var comp = target.getComponent(compType);
if (!cc.isValid(comp)) return;
var handler = comp[this.handler];
@@ -143,5 +156,23 @@ cc.Component.EventHandler = cc.Class({
}
handler.apply(comp, params);
}
},
_compName2Id (compName) {
let comp = cc.js.getClassByName(compName);
return cc.js._getClassId(comp);
},
_compId2Name (compId) {
let comp = cc.js._getClassById(compId);
return cc.js.getClassName(comp);
},
// to be deprecated in the future
_genCompIdIfNeeded () {
if (!this._componentId) {
this._componentName = this.component;
this.component = '';
}
},
});