Agile Toolkit JavaScript PHP

Agile Toolkit JavaScript Digest

The purpose of JS chains in Agile Toolkit is to provide a glue between JavaScript and PHP. It is NOT to completely replace native JavaScript.

Agile Toolkit offers a better way to organise your JavaScript code. It is by putting it inside a jQuery namespace. The way you call your function will be slightly different:

// Before

// After

// From PHP


Extending The Universal library

make a .js file in templates/js and put these codes:

    myfunc: function(a){
        alert('myfunc: '+a);
    mysum: function(foo,bar){
        return foo+bar+1;

now you can call js like this:

$page->add('Button')->set('Click Me')
    ->univ()->myfunc('hello world');

Static Includes

If you are including some 3rd party code, they sometimes might not like dynamic loading. Third party code might assume that the on-ready is not yet triggered or do some other kind of magic. Google Maps code, for example, wants to be loaded along with your page.

If this happens, you can specifically instruct Agile Toolkit to put the include inside the HTML.

// or

You must be mindful to include this on the page, which is loaded directly, and can potentially open other pages through AJAX, requiring this include. If unsure, add this code to your API’s init() method.

If you want to go even more ‘hardcore’ with the include you can do this:

    '<script type="text/javascript" src="http://..."></script>'."\n");

Executing any JS code

One of the goals of JS Chains is security. JavaScript injection is a common problem and by having a nice interface between your JavaScript and server backend you become immune to it.

If you think that you know better, you can execute a string of JavaScript code from anywhere like this:

// DANGER: Contains JavaScript injection vulnerability

// SAFE: $my_message is escaped



Leave a Reply

Your email address will not be published. Required fields are marked *