Factory class loads class definitions through AJAX and attempts to create an object that is being requested. Libraries can also be loaded dynamically as well as functions called from that library with a single request. Stylesheet loading creates a new style resource in HTML head section.
Once this is done, I can load the class easily with the following:
// Create a new Factory class var Factory=new WWW_Factory(); // Get new instance of the 'example' class var myExample=Factory.getObject('example'); // And now you can use that object like any other myExample.doSomething();
Same applies to libraries. If I have a library of functions in a script that is situated in '/resources/libraries/my-stuff.js', then I can dynamically load that library and use functions from it like this:
// Factory class is required var Factory=new WWW_Factory(); // Load the library Factory.loadLibrary('example'); // Use some function from that library doSomething();
Note that you have to be careful when loading functions to global namespace in the browser, such as when loading libraries and classes. It is useful to use some naming standard or prefixes for your classes and functions to reduce possible namespace conflicts.
This is the base URL where class files are loaded from. Factory attempts to load classes from that folder. This value can be overwritten when defining a different URL when initializing Factory class.
This is the base URL where library files are loaded from. Factory attempts to load libraries from that folder. This value can be overwritten when defining a different URL when initializing Factory class.
This variable simply holds information about which files have been already loaded to make sure that the same classes and libraries are not loaded multiple times.
Factory class takes only one variable 'base' which defines the root folder where the Factory looks for the script files. Default location is base URI subfolders in /resources/ folder.
This method loads and returns a new object for a class 'className'. Factory attempts to load this class definition from a file in /resources/classes/ and file name should be written in class.[className].js format in that folder. If 'methodName' is set, then this method will also automatically call that method and return result of that method instead of the view object itself. If 'methodData' is also set, then this is the input variable sent to methodName.
This method is used to dynamically load a stylesheet from /resources/styles/ subfolder.
This method is used to dynamically load a library from /resources/libaries/ subfolder. If additional parameters are set, then this method can also automatically call one of the functions in the library.
This private function is used to load contents of a script file and initialize these contents in the browser for use as classes, functions or other scripts.