JavaScript API Wrapper Class

Files

/engine/class.www-factory.js

Introduction

JavaScript factory has very minimal use compared to the Wave Framework's own internal PHP Factory, but this class is still useful for dynamically loading JavaScript objects from /resources/classes/ folder or libraries from /resources/libraries/ subfolder and stylesheets from /resources/styles/ subfolder.

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.

Using JavaScript Factory Class

By default the factory class loads scripts from '/resources/' folder and '/classes/' and '/libraries/' subfolders. For the dynamic object loading to work, the class name and filename must include the same keyword. If I create a new JavaScript class 'example', then I need to place a file 'class.example.js' to '/resources/classes/' subfolder and make sure that my class name is 'example'.

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.

JavaScript Factory Class Parameters

var classBase=document.baseURI+'resources/classes/'

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.

var libraryBase=document.baseURI+'resources/libraries/'

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.

window['WWW_Factory_loaded']=new Object()

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.

JavaScript Factory Class Methods

function WWW_Factory(base)

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.getObject=function(className,methodName,methodData)

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.loadStyle(styleName)

This method is used to dynamically load a stylesheet from /resources/styles/ subfolder.

this.loadStylesheet=function(libraryName,functionName,functionData)

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.

var loadFile=function(address)

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.