PDA

View Full Version : Ext.Loader and Custom Paths



kell
9 Oct 2013, 6:39 AM
I'm looking for a better, more proper, way of getting dynamic class loading to work in my environment. We are loading javascript files using a web service, so, the files are not coming from a specific directory structure. For example, if I require a class named NS.ns1.ns2.ClassName, it will be loacated at http://domain/path/ns_ns1_ns2_classname. Basically, what would normally be slashes, are converted into underscores.
It would be nice if the Loader would call a custom method for specific namespaces, but I don't think that is possible.
Here is the code I have used to override the Ext.Loader.getPath method to do what I need. Any ideas on improvement are appreciated.



// define some global variables so the getPath method can operate
Loader = Ext.Loader;
slashDotSlashRe = /\/\.\//g
dotRe = /\./g

// redefine the getPath method of the Ext class loader object so it can work in our environment
Ext.Loader.getPath = Ext.bind(function(className) {
var path = '',
paths = Loader.config.paths,
prefix = Loader.getPrefix(className);

if (prefix.length > 0) {
if (prefix === className) {
return paths[prefix];
}

path = paths[prefix];
className = className.substring(prefix.length + 1);
}

if (path.length > 0) {
if(path == '/NS'){
path += '_';
path = path.replace(slashDotSlashRe, '/') + className.replace(dotRe, '_');
if(!Loader.config.disableCaching){
path += (qsObj.debug ? '?debug=true' : '');
}
return path;
}
path += '/';
}

return path.replace(slashDotSlashRe, '/') + className.replace(dotRe, '/') + '.js';
}, Ext.Loader);

// setup the ext class loader
Ext.Loader.setPath('NS', '/NS');

I'm now able to reference a class and have it loaded in my environment, but this does not seem like an elegant hack.

ettavolt
10 Oct 2013, 1:28 AM
What for do you need dynamic class loading?
If your app cannot live without loading, than I would say that your approach is ok.