mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
componentEventHandler serialize compId instead of compName (#2973)
This commit is contained in:
@@ -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 = '';
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user