PDA

View Full Version : How to lazy load a class but not create an object of that class (i.e. to get statics)



jonathan.ingram
23 Aug 2011, 5:21 PM
Say I have a class "Ext.ux.Foo" and it has statics, how do I retrieve the class without creating an object. Obviously I do Ext.create("Ext.ux.Foo", {}) to create an object, but I have to do Ext.ux.Foo.statics.bar to get the static property "bar". This works, but it means I need a "requires" for the class "Ext.ux.Foo" otherwise the class does not auto load.

Is there an Ext.getClass-type method that would simply return the class?

skirtle
23 Aug 2011, 5:38 PM
I think you may be worrying about this unnecessarily. Why not just use Ext.require()? I don't believe the Ext loader is really intended as a 'lazy loading' mechanism. Indeed, any attempt at lazy loading will cause an application to become unresponsive to users.

Off the top of my head I can think of two use cases for using the loader in an application:

Easier debugging during development as all the JS files are kept separate.
A quick alternative to having to make a custom ExtJS build for a production application that has a very small number of JS dependencies. The only concrete example of this I can think of is the ExtJS demo suite.

jonathan.ingram
23 Aug 2011, 5:49 PM
BTW, I do combine the files for production and use the Ext.loader for development.

I suppose it's fine to use requires (BTW, I'm talking about this type of requires):


Ext.define("...", {
requires: []
});

I guess what I'm saying is that you don't need to use requires for creating objects because Ext.create will load the class if it's not loaded, and it would be nice if there was a similar method for getting the class.

Also, probably a minor issue is that if I have a class name such as Ext.ux.really.really.really.long.Name I am having to put this in the requires: [] and also when I want to access the static property.

Anyway, if no such method exists, that's fine, just thought it might :) and we can probably close this thread!

Cheers,
Jon

skirtle
23 Aug 2011, 6:12 PM
You could write it yourself. Haven't tested it but I'm guessing it'd look something like this:


MyNamespace.getClass = function(name) {
Ext.syncRequire(name);

return Ext.ClassManager.get(name);
};


...we can probably close this thread!

To close the thread you need to mark it as Answered.

jonathan.ingram
23 Aug 2011, 6:14 PM
OK thanks for the help, although I suspect that name would conflict with the existing Ext.getClass(component). Cheers, Jon.