Score:0

Overriding Drupal.ajax.prototype is not working

bn flag

I am trying to override Drupal.ajax.prototype.beforeSend like this:

(function($) {
  // In mymodule.js, which appears after ajax.js in the page build
  Drupal.ajax.prototype.beforeSend = function(xmlhttprequest, options) {
    console.log('overridden!')
  }
})(jQuery);

The file that contains the code is listed below core's ajax.js in the page's source, yet it's the console.log I put inside core's ajax.js beforeSend that is showing up.

What else is to it? Has anything been changed in Drupal? Using version 9.3. Can't override any of those functions.

Kevin avatar
in flag
Did you also pass in the 'Drupal' object?
KrzysiekK avatar
bn flag
I even passed 'jQuery, Drupal, drupalSettings'.
4uk4 avatar
cn flag
Isn't `ajax` spelled `Ajax`?
KrzysiekK avatar
bn flag
Wow, actually there are both "ajaxes", and the one I need is, indeed, spelled 'Ajax'. Thank you! Can't mark the comment as the answer though.
Score:1
cn flag

There are two javascript objects:

  1. Drupal.ajax
  2. Drupal.Ajax

You have to override the prototypes in Drupal.Ajax. For each Ajax object declared in Drupal settings an instance of it will then be stored in Drupal.ajax.instances.

sonfd avatar
in flag
Do you know where this is documented? I looked around a little and I mostly just find answers to questions like this, with information like this, but no linked source.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.