Core Javascript code examples

This document describes the core Javascript API to be used in the generic UI pages

It applies to either:

Note that you can use in either case the other Javascript APIs such as:

Page events

Page loading

A Javascript function can be bind to the page load event (it uses jQuery's bind() on the "load"" event and the added functions are called after the standard load event functions of the generic UI):

Example 1 (using a constant):

onload_functions.push(function() {
    console.log("Welcome to Simplicite version " + Simplicite.VERSION);
});

Example 1 (using the Ajax API singleton):

onload_functions.push(function() {
    Simplicite.Application.getGrant(function(g) {
        console.log("Welcome " + g.login + "!");
    });
});

Note: You can also use jQuery $(document).ready() but the functions will be called before the ones added as above and therefore before the standard load event functions, some standard stuff may not be initialized at that stage. The use of readyis thus not recommended unless you know what you do. _

Page before unloading

A Javascript function can be bind to the page beforeunload event (it uses jQuery's bind() on the "beforeunload"" event and the added functions are called after the standard beforeunload event functions of the generic UI):

Example:

onbeforeunload_functions.push(function() {
    return "Are you sure ?";
});

Page unloading

A Javascript function can be bind to the page unload event (it uses jQuery's bind() on the "unload"" event and the added functions are called after the standard unload event functions of the generic UI):

Example:

onunload_functions.push(function() {
    console.log("Goodbye !");
});

Custom Javascript patterns

It is highly recommended to write your client Javascript code in unique namespaces.

For instance you can use the object name MyObject as namespace (among advantages of this pattern you have private functions, per-business object naming unicity, etc.):

E.g.:

MyObject = (function MyObject($) {
    function myPublicFunction() {
        // Here I can use $ for jQuery
    }
    function myPrivateFunction() {
        // Here also
    }
    //...
    return { myFunction: myPublicFunction };
})(jQuery);

Then tou can use calls like MyObject.myFunction().