I am trying to ajaxify my site. I want the “Main page content” to be passed via an ajax-command to a custom js-function.
To accomplish this I would do the following:
- I need to register a path for my ajax requests
code:
$items['_custom_ajax_'] = array( 'title' => 'AHAH callback', 'page callback' => 'ajax_form_callback', 'delivery callback' => '_custom_ajax_deliver', 'access callback' => TRUE, 'theme callback' => 'ajax_base_page_theme', 'type' => MENU_CALLBACK, );
- use a custom delivery callback function
_custom_ajax_deliver
which is the copy of theajax_deliver()
original code:
function ajax_deliver($page_callback_result) { // Browsers do not allow JavaScript to read the contents of a user's local // files. To work around that, the jQuery Form plugin submits forms containing // a file input element to an IFRAME, instead of using XHR. Browsers do not // normally expect JSON strings as content within an IFRAME, so the response // must be customized accordingly. // @see http://malsup.com/jquery/form/#file-upload // @see Drupal.ajax.prototype.beforeSend() $iframe_upload = !empty($_POST['ajax_iframe_upload']); // Emit a Content-Type HTTP header if none has been added by the page callback // or by a wrapping delivery callback. if (is_null(drupal_get_http_header('Content-Type'))) { if (!$iframe_upload) { // Standard JSON can be returned to a browser's XHR object, and to // non-browser user agents. // @see http://www.ietf.org/rfc/rfc4627.txt?number=4627 drupal_add_http_header('Content-Type', 'application/json; charset=utf-8'); } else { // Browser IFRAMEs expect HTML. With most other content types, Internet // Explorer presents the user with a download prompt. drupal_add_http_header('Content-Type', 'text/html; charset=utf-8'); } } // Print the response. $commands = ajax_prepare_response($page_callback_result); $json = ajax_render($commands); if (!$iframe_upload) { // Standard JSON can be returned to a browser's XHR object, and to // non-browser user agents. print $json; } else { // Browser IFRAMEs expect HTML. Browser extensions, such as Linkification // and Skype's Browser Highlighter, convert URLs, phone numbers, etc. into // links. This corrupts the JSON response. Protect the integrity of the // JSON data by making it the value of a textarea. // @see http://malsup.com/jquery/form/#file-upload // @see http://drupal.org/node/1009382 print '<textarea>' . $json . '</textarea>'; } // Perform end-of-request tasks. ajax_footer(); }
-
create an custom function which do the ajax request like
Drupal.ajax
-
bind the ajax request to some selector e.g.
a.ajaxify
-
add a custom ajax-command
Im not sure if this is the right way, or if I can extend the core easier somehow. Basicly my approach is simmilar to the module Ajaxify Drupal with JQuery Ajax
Better Ideas?
Update: one more thing – I need an event to be triggered everytime an ajax-link is clicked.