JQLoader Examples

The following examples will give you an idea about how to use jQLoader.

Creating a Simple Class

To register a class simple invoke the .register() API from the $.jqloader plugin. This method takes two parameter. The first one is a key identifier for the class which you will use to request an pointer of the constructor of the registered class; the second one can take an annonymous object with properties and methods or the construstor of your class.

Example #1

Example #2

Example #3

Getting a Pointer to the Class Constructor

The .get() API will help getting a pointer to the constructor of a registered class.

Including Dependencies

A class dependency must be declare before registering the main primary class. You should use .include() API to let jQLoader know that one or many dependencies are required before flagging the primary class as loaded.

Loader code:

HelloApp.js code:

Hello.js code:

Employee Class

The following example demostrate how to load a class and create an inistance of it. The Employee class borrows the method .property from the class Person which is loaded automatically.

Extending Classes (pseudo-inheritance)

jQLoader allows borrowing member of other classes using the .borrow() API. This is not formal inheritance, but practically speaking it accomplish similar results in a easy way. The .borrow() API can be invoked at any time, but it makes sense to borrow methods and properties from the class constructor. Only the members that have not been defined in the borrower class will be added.  In the example notice that the .methodA() but the value of the key has been already set in ClassB. In some this is similar to an overwrite. Members defined in the borrower take priority over those in the borrowed classes.

Loader code:

ClassB code:

ClassA code:

Class Mashup (multiple-inheritance)

You can borrow members from multiple class by passing an array with the keys of the classes you would like to borrow from. When doing so jQLoader will process the classes in the order that they appear in the array. If a member is defined in the first class that you are borrowing from this will not be overwriten by any of the subsequent classes in the array.

Loader code:

ClassC code:

ClassB code:

ClassA code:

Creating an Application Class

Any class could be an application class. There are two conditions required to create an application class: first, that class must have a .main() method that will be automatically called when you run your application; second, the class must be registered as Application.

To run your application you should use the .run() API and pass the path to the Javascript file that contains you application main class. You can also pass an custom Object from the HTML to the .main() method of your application as a second parameter in the .run() method.

The goal of an application class is to make easier to startup your application and remove all the initialization code from the HTML page that host you Javascript code. The following examples show a simple application class and the snipped of code that goes in you HTML page. Notice that in this example SimpleApp includes ClassC which includes ClassA and ClassB.

Loader code:

SimpleApp.js code

Using Custom Options to Simulate Class Packages

When you load, run, or publish a class you can pass an additional parameters to control the behavior of jQLoader.

You can also include custom parameters that will be passed to the .main() method of your application class or to the .ready() callback function that you had specified. In this example the baseURL has been set to ‘src/’ (must include a slash at the end), which means that all classes will be laoded from that location. In addtion to this the useClassPath variable has bee set to true. This instruct jQloader to treat any class source as a full qualify path. This combination of settings will allow you to move all your classes to a different location (i.e from a dev server to a production server) without modifiying your class code.

In this example jQLoader has been instructed to load the class ‘com.sanraul.demo.Ping’. Since the baseURL has been set to 'src/' and the useClassPath flag is set to true, the .load() request will resolve the class path as 'http://localhost:8080/src/com/sanraul/demo/Ping.js.

jQLoader Options

  • autorun {Boolean}
  • useClassPath {Boolean}
  • baseURL {String}
  • error {Function}
  • ready {Function}

Loader code:

Ping.js code

Leave a Reply