Introduction
One of the main problems with ajax is that the page doing the calling and the page doing the answering must belong on the same (sub)domain and, at the moment, there's no native way to do this within the evocore as plugins only have a htsrv method which can often be on a seperate (sub)domain from the blog.
This plugin cures that problem and makes ajax requests simple in the process! How it works is pretty easy, it hooks into the [i]session loaded[/i] event and looks to see if it's an ajax request or not. If it is then it calls all the plugins that have asked to be notified of the particular ajax request.
Every ajax plugin could hook into the [i]session loaded[/i] event and check for it's own requests but if more than one or two were used on the same blog then the overheads would soon mount up as each of the plugins would have to be instantiated for every single page request! Not a cpu friendly situation
Besides making ajax requests as cpu friendly as possible, this plugin also takes the majority of the hard work out of the loop for other ajax plugins. Making a request is as simple as one line of code, no longer do you have to work out how to create an Http object, and make sure that your names don't clash with every other ajax plugins names.
This plugin has been tested in InterdebtExploiters 6 & 7, Firefox 1.5.0.7 and Opera 9.02, and guess what? It works in them all :D
Download and installation
Installation
- DOWNLOAD: Am Ajax v 1.0 (zip) and unzip it locally.
- Copy the am_ajax_plugin folder into the "plugins" folder of your b2evolution installation.
- Login to the administrative interface for your blog.
- Install the Am Ajax plugin from the Settings > Plug-ins > Available plugins table using the [Install] link.
To ensure that the user has the latest version of this plugin we ask that you don't ship it with your own plugin, but instead link to this page.
Dependencies and events
So, how do you use it? Well, you obviously need this plugin installed before it can do anything, so if you're coding an ajax plugin, and you want to use this helper plugin as a go between, you first need to add it to your plugins dependencies. This will ensure that it will be installed and enabled before your plugin can be installed. You do this by adding the following code to your plugin :-
function GetDependencies()
{
return array(
'requires' => array(
'plugins' => array( array( 'am_ajax_plugin', '1' ) ), // at least version 1 of am_ajax_plugin
),
);
}
The next step is to register all of the ajax events that your plugin needs to be notified of. The best place to do this is during the installation process and is as simple as calling this plugin with a list of events. For example, the following code will add [b]am_ajax_skin[/b] to the events list :-
function AfterInstall()
{
global $Plugins;
$ajax = $Plugins->get_by_code( 'am_ajax' );
$params[ 'id' ] = $this->ID;
$params[ 'events'] = array( // array of all the ajax events your plugin responds to
'am_ajax_skin' );
$Plugins->call_method( $ajax->ID, 'AddAjaxEvents', $params );
}
If you're one of those tidy people you can also remove your plugins events during your uninstall procedure in pretty much the same manner with the following code :-
function BeforeUninstall()
{
global $Plugins;
$ajax = $Plugins->get_by_code( 'am_ajax' );
$params[ 'id' ] = $this->ID;
$params[ 'events'] = array( // array of all the ajax events to remove, or an empty array to remove all events
);
$Plugins->call_method( $ajax->ID, 'RemoveAjaxEvents', $params );
}
You can also use the Add/Remove methods if your plugin is upgraded a later date. Only new events will be added so you don't have to worry about duplicates and, as you can see from the snippet above, you can choose to either remove all events or just selected events.
Making requests
Now that you've got the plugin installed and registered your plugins ajax events, how do you use them? Simple, you just call the ajax request function and it does all the work! You can pass it any additional parameters that your request requires and even get it to show a wait screen, all with just one line of code :-
amAjaxRequest( 'theRequest', 'theParameters', theResponse, showWait, 'theData' );
So what does the above snippet mean? [b]theRequest[/b] is the ajax event that your plugin previously registered (am_ajax_skin in our example), [b]theParameters[/b] is a list of any additional parameters in the same format as a url ( ie/ the am_ajax_skin event could be passed a post id and be asked for comments with [i]'p=123&c=1'[/i] ), [b]theResponse[/b] is the name of your javascript function that will handle the returned data, [b]showWait[/b] is a true/false flag for displaying the wait screen, [b]theData[/b] is any additional data that your response function may require and can be anything you like!
Time for a working example. The following snippet makes an ajax request and displays the results on the screen when you [b]Click me[/b]
<span onclick="yourName= prompt( 'what is your name ?');amAjaxRequest( 'am_ajax_demo', 'name=' + yourName, amDemoResponse, true, 'some data' );">Click me</span>
<script type="text/javascript">
function amDemoResponse( theReply, myData )
{
window.alert( 'the server said : ' + theReply + ' - and I also got the following data from the ajax helper : ' + myData );
}
</script>
[script]function amDemoResponse( theReply, myData )
{
window.alert( 'the server said : ' + theReply + ' - and I also got the following data that was stored for me : ' + myData );
}[/script]
Another page
Some title
Recent Comments