Ajax Load
Helper module to load and add JavasScript and CSS data for Ajax-loaded content.
When loading new content via AJAX, there is the potential need to load CSS and Javascript files and data not already available on the page. Ajax Load is a helper module designed to handle this task. It should only be installed for use with another module.
Ajax Load was written to accompany a patch that added AJAX loading to the Views module. When enabled, Ajax Load will ensure that any needed JS and CSS files are loaded along with dynamically loaded views.
Developer usage
Ajax Load implements a drupal_alter() hook, hook_ajax_data_alter().
Ajax Load expects an object that is about to be sent in JSON format. The object should have a __callbacks property with Javascript callbacks that the JSON data are to be fed into. Here is the relevant code from ajax.inc in Views:
<?php
// Register the standard JavaScript callback.
$object->__callbacks = array('Drupal.Views.Ajax.ajaxViewResponse');
// Allow other modules to extend the data returned.
drupal_alter('ajax_data', $object, 'views', $view);
?>In this snippet we first set a __callbacks property on the object to be returned and then pass it through drupal_alter(). As well as the object, we pass two additional optional arguments: an object type argument - in this case, 'views' - and the original object, in this case, the view. These optional arguments can be used in hook_ajax_data_alter() implementations to help decide what alterations to apply.
On the Javascript side, the AJAX submit handler must call the callbacks specified in the __callbacks array. Here is the relevant code from ajax_view.js:
.submit(function () {
$('input[@type=submit]', this).after('<span class="views-throbbing"> </span>');
$(this).ajaxSubmit({
url: ajax_path,
type: 'GET',
success: function(response) {
// Call all callbacks.
if (response.__callbacks) {
$.each(response.__callbacks, function(i, callback) {
eval(callback)(target, response);
});
}
},
error: function() { alert(Drupal.t("An error occurred at ") + ajax_path); },
dataType: 'json'
});
return false;
})Note that, in the success handler, we call all registered __callbacks.
Ajax Load makes the following alterations to an object it is fed:
- add arrays of Javascript and CSS data/files
- add a Javascript callback to receive the returned data
When the JSON data are returned, Javascript and CSS files are examined and, if not already present, are loaded. All going well, Javascript behaviours and CSS instructions should be present as needed for the dynamically loaded content to function and display correctly.
Releases
| Development snapshots | Date | Size | Links | Status | |
|---|---|---|---|---|---|
| 6.x-1.x-dev | 2008-Jul-02 | 7.2 KB | Download · Release notes | Development snapshot | |
