0

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:

  1. 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,
);
  1. use a custom delivery callback function _custom_ajax_deliver which is the copy of the ajax_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();
}
  1. create an custom function which do the ajax request like Drupal.ajax

  2. bind the ajax request to some selector e.g. a.ajaxify

  3. 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.

0

IMHO, I'd like to use Drupal AJAX API instead of jQuery AJAX because it supports many commands, events and content are manageable, error control and event binding for new content, those features are supported by Drupal.

But the disadvantage is it sends too much data, process is more complicated, using POST all the times. But of course, at javascript level, we can override the default behaviors and make it better :)

For example, If you have any ajax errors, like ajax request is running and you change document location, you will see an alert box with error message. That's a bit annoying so I always override it by re-write this function in my theme

Drupal.ajax.prototype.error

About your working process, I have some comments

  • 1 & 2: It's ok for me, but why do you need to use another copy of ajax_deliver(), why don't you use it directly?
  • 3: Are you going to write another thing like Drupal.ajax or just attach new function to Drupal.ajax? something like

    Drupal.ajax.prototype.commands.log = function (ajax, response) {
        console.log(response);
    };
    
    Drupal.ajax.prototype.commands.open_popup = function (ajax, response) {
        var id      = typeof response.id      === 'string' ? response.id : 'gfc';
        var content = typeof response.content === 'string' ? response.content : '';
        var options = typeof response.options === 'object' ? response.options : {};
    
        Gfc.popup.open(id, content, options);
    };
    

    I'm ok if you do like that.

  • 4: Yes of course

  • 5: Errr, are you going to write something new instead if using Drupal.ajax?
  • thx for your time, I would like to specify some things: 1. ajax_deliver() uses the commands from the function ajax_prepare_response() which uses $commands[] = ajax_command_insert(NULL, $html); $commands[] = ajax_command_prepend(NULL, theme('status_messages')); as standard. I dont want my response to be insered directly. I want to store it, do some javascript before (animation, validation..) and finnaly parse it. if i use the default Ajax – anonym-developer Sep 17 '14 at 9:40
  • this is the reason why I want to create a custom command … then I can process my response my way. to trigger the command, I thought I need to override ajax_deliver() / ajax_prepare_response(). – anonym-developer Sep 17 '14 at 9:45
  • * if i use the default Drupal.ajax, the request goes to system/ajax, and the callback to ajax_deliver(). The question is, if I can do some other callback. – anonym-developer Sep 17 '14 at 9:47
  • 1
    No, you still can use another url. Open misc/ajax.js and you'll see inside the $('.use-ajax-submit:not(.ajax-processed)').addClass('ajax-processed').each(function () {});. You can define your own url, event to bind – Nhat Sep 17 '14 at 12:24
  • 1
    about the command, why don't you do like this: Drupal.ajax.prototype.commands.doMyStuff = function () { // Animation // Fancy stuffs // Amazing features // Main logic // Show up to user } – Nhat Sep 17 '14 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.