Half year ago we have introduced jQuery / Chain into AModules3 (former name for Agile Toolkit). It allows us to clean up a lot of older code and at the same time give developers a way to do many new things in a flexible way.
How things were before?
Library was called amodules3.js. To rely on those functions form got support for “onclick”, and so did buttons. That was very clumsy.
- added <…id=> to all view elements such as grid, form etc.
- all the interactions were done through binding $(…).click(). and so on.
So now when page or part of the page is being generated through render(), it also puts together list of js-actions to perform on those elements.
Basic approach and PHP integration.
$button->js(‘click’)->univ()->dialogURL(‘Add new item’,$this->api->getDestinationURL(‘./add’));
but also a custom functions are quite useful:
Resulting code will tell text to reload when button is clicked. You can also use chains to get values such as:
return $button->js(‘click’)->univ()->alert( $form->getElement(‘name’)->js()->val() );
Code above will display alert message containing current value of form field in it.
Two types of AJAX requests
Agile Toolkit do not burden developers with complex protocols. There are only two ways how AJAX requests are been passed.
- regions – Request to load part of the page. Typically complimented by arguments such as cut_region, cut_object or cut_page. Region would contain valid piece of HTML code, but will also contain <script> tag which have to be properly executed by ajax function doing the request. atk4_load() and univ().page() would typically be sending this kind of request.
You have to be aware what type of request you will be receiving. If you are supposed to return js-code you should do so through the following construction:
The final function execute() will echo the chain and will stop execution of PHP script. This is typically done to produce decision quickly and do not waste too much time on generating reply.
To be continued…