Skip to content

Instantly share code, notes, and snippets.

@henrytran9x
Created June 15, 2016 01:46
Show Gist options
  • Save henrytran9x/e86c3a5554f54df80b6e73a6e71a4e7c to your computer and use it in GitHub Desktop.
Save henrytran9x/e86c3a5554f54df80b6e73a6e71a4e7c to your computer and use it in GitHub Desktop.
Add more triggers in ajax.js.

There have been a couple instances recently where I wanted to fire some custom handlers when particular events in ajax.js were invoked. It's possible I'm a bit off track with this and there may be an alternate solution, as admittedly jQuery is not my strongest language.

To give an example, suppose you wanted to manipulate a page element when Drupal.ajax.prototype.beforeSend is called. Up to this point, I would handle this by adding something along these lines in a custom .js file:

(function ($) {

// Drupal's core beforeSend function var beforeSend = Drupal.ajax.prototype.beforeSend;

// Add a trigger when beforeSend fires. Drupal.ajax.prototype.beforeSend = function(xmlhttprequest, options) { beforeSend.call(this, xmlhttprequest, options); $(document).trigger('beforeSend'); }

})(jQuery);

Then, use $(document).bind('beforeSend', ...); where necessary.

A use case could be when exposed filters on an ajax-enabled view are submitted, you want to fade the view-content before it's replaced.

What I am proposing is possibly adding these triggers directly in ajax.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment