How to resolve circular dependency in editor cocos creator?

How to resolve circular dependency in editor cocos creator?
0

#1

For resolving circular dependency in code I use following approach:

classB.js:

var classA;

var classB = cc.Class({
 extends: cc.Coponent,

someMethod:function( node ){
 node.getComponent( classA );
}
});
module.exports = classB;
classA = require("classA");

classA.js:

var classB;

var classA = cc.Class({
 extends: cc.Coponent,

someMethod:function( node ){
 node.getComponent( classB );
}
});
module.exports = classA;
classB = require("classB");

And it works properly but when I need to use classB as a type of parameter in classA like here:

classA-2.js:

var classB;

var classA = cc.Class({
 extends: cc.Coponent,
parameters:{
 param:{
 default: [],
 type: [ classB ]
}}
});
module.exports = classA;
classB = require("classB");

CocosEditor shows me warning: “The “type” attribute of “classB.param” is undefined when loading script.” and editor does not allow me to fill in the parameter. The field of “param” in script component parameters look like on the picture below…

Somebody who know how to resolve it?
Thanks for replying!


#2

i still remember we could find the cross-reference solution sample in cocos creator buildin sample project.
the sample-core path in sample project is path_to_sample_project\assets\cases\05_scripting\05_cross_reference

the solution is simple
just set the proerties obj by arrow function
so it would delay the properties config

in B-script

cc.Class({
extends: cc.Component,

properties: () => ({
    refToA: require('A-script')
}),

// use this for initialization
onLoad: function () {
    let refToA = this.refToA;
}

});

in A-script

cc.Class({
extends: cc.Component,

properties: () => ({
    refToB: require('B-script')
}),

// use this for initialization
onLoad: function () {
    let refToB = this.refToB;
}

});