PDA

View Full Version : Dynamic loading using "requires"



phamtranquocviet
18 Feb 2012, 6:08 AM
Hi,

I am trying to implement access control for my web desktop. Each logged user has different menu under start button. Here is how I did it:

My login.php returns an array of class names to be loaded as module inside web desktop example like this: ["MyDesktop.VideoWindow", "MyDesktop.SystemStatus",...] <= this is the "acl" used below

I instantiate, apply the above array to the instantiated object, and modify getModules as followed:
desktop = Ext.create("MyDesktop.App");
Ext.apply(desktop, {acl: JSON.parse(acl).acl});

getModules : function(){
var modules = new Array();
for(i = 0; i < this.acl.length; i++) {
modules.push(Ext.create(this.acl[i]));
}
return modules;
},

That works fine when "requires" is static (requires: [classA, classB, ...]). However, how do I make the "requires" dynamically load classes returned by my login.php? Meaning, how do I do something like this:

requires: this.acl // Does not work because the above apply call is not yet executed at the time "requires" is trying to load.

Thanks.

phamtranquocviet
18 Feb 2012, 6:56 AM
OK,

I took out the apply call, wrote a constructor, commented out the init function inside App.js as followed:

//How I instantiate outside of App.js
Ext.create("MyDesktop.App", {acl: JSON.parse(acl).acl});

//Code inside App.js
constructor: function(acl) {
Ext.apply(this, acl);
console.log(this.acl); // correctly shows [MyDesktop.VideoWindow,..]
Ext.require(this.acl);
this.callParent();
},

I got this error:

c is not a constructor
....instantiators[length] = new Function('c', 'a', 'return new c('+args.join(',')+'...

I now have no clue what this error means. Please help. Thanks.