Hello AJAX World

Matt Thomas (a.k.a betweenbrain)
http://betweenbrain.com/talks/hello-ajax-world

Joomla Ajax Interface

Core component as of Joomla 3.2

Joomla 1.5 / 2.5 version
Sample modules and plugin available at https://github.com/Joomla-Ajax-Interface

Origin

Inspired by a client, fostered by community.

Resolves limitation of requests to modules and plugins.

AJAX in 30 Seconds (or less)

A group of web technologies to send and retrieve data from a server asynchronously without interfering with the page.

The Obstacle

In Joomla, a request must always be a component, can never be a module or plugin.

(think menu items)

The Challenge

Create a stand-alone module where no component supports Ajax functionality.

The Solution

Make requests to modules and plugins via a component.

AJAX Example (jQuery)


    (function ($) {
     $.ajax({
         data   : {
             'option' : 'com_ajax',
             'plugin' : 'hellokitty',
             'format' : 'json'
         },
         success: function (response) {
             console.log(response)
         }
     });
 })(jQuery)

Anatomy of an Ajax Request

Required

Call the component

option=com_ajax

Designate the target

[module|plugin]=name
(e.g. module=session for mod_session)

Response format

format=[json|debug|raw]

Module URL Parameter

Path of helper file

$helperFile = JPATH_BASE . '/modules/mod_' . $module . '/helper.php';

Class name

$class      = 'mod' . ucfirst($module) . 'Helper';

Optional

Alternate module method prefix.

method=[custom fragment]
(defaults to 'get', suffix always 'Ajax', e.g. 'getAjax')

&module=session

File Structure


		└── modules
		  └── mod_session
		    ├── helper.php
		    ├── mod_session.php
		    ├── mod_session.xml
		    └── tmpl
		      └── default.php
								

helper.php


        <?php defined('_JEXEC') or die;

    class modSessionHelper {

        public static function getAjax()
        {

            return 'Hello Ajax World';
        }
    }
								

Plugin URL Parameter

Plugin event to trigger

$response   = $dispatcher->trigger('onAjax' . $plugin);

&plugin=latestarticles

File Structure


		└── plugins
		  └── ajax
		    └── latestarticles
			 ├── latestarticles.php
			 └── latestarticles.xml
		

latestarticles.php


        <?php defined('_JEXEC') or die;

        class plgAjaxLatestarticles extends JPlugin {

            function onAjaxLatestarticles() {

            }
        }
								

Additional Data

Anything your plugin or module needs. For example:

cmd=add
data=foo
debug=true

To Code!

https://github.com/Joomla-Ajax-Interface/Hello-Ajax-World-Module

Thank You

Matt Thomas (a.k.a betweenbrain)

http://betweenbrain.com/talks/hello-ajax-world/

https://github.com/joomla-ajax-interface