Drupal.ajax event onload?

Created on 17 May 2023, over 1 year ago
Updated 17 May 2024, 8 months ago

Problem/Motivation

We have a view that returns several related nodes which used entity view attachments (EVA) to pull in related audio media. Unfortunately, all these entities would cause unacceptably long page loads, so I off-loaded the EVAs into Views AJAX calls.

We now have a DIV the end-user clicks for each media to execute the View AJAX to load that audio player:

/** 
 * Attaches an on-click event allowing us to load the video_media_evas:smallest
 * view into a div with the classes "av_ajax_player" and "js-view-dom-id-av-player-{{nid}}"
 * with the node's ID in the data-nid attribute.
 * E.g. `<div class='av_ajax_player js-view-dom-id-av-player-{{nid}}' data-nid={{nid}}>PLAYER!</div>`
 */

(function (Drupal, once) {
  Drupal.behaviors.ajaxViewDemo = {
    attach(context) {
      once('attach-player', '.av_ajax_player', context).forEach(
        element => {
          // Pull node ID from div.
          var nid = element.attributes['data-nid'].value;

          // Everything we need to specify about the view.
          var view_info = {
            view_name: 'video_media_evas',
            view_display_id: 'smallest',
            view_args: nid,
            view_dom_id: 'av-player-' + nid
          };

          // Details of the ajax action.
          var ajax_settings = {
            submit: view_info,
            url: '/views/ajax',
            element: element,
            event: 'click'
          };

          // Have drupal/ajax attach the on-click event.
          Drupal.ajax(ajax_settings);
        }
      );
    },
  };
}(Drupal, once));

The client asked if we can have the page go ahead and just run each view AJAX call right away instead of making the user click the DIV. Unfortunately, simply changing my event: 'click' line to event: 'load' (or 'onload') doesn't work because, as far as I can tell, the onload event only works if linked to the document or window.

Am I missing something obvious that would allow Drupal.ajax to load the audio players right away? Most of the documentation assume forms or links, so I'm not finding anything that quite matches my use case.

πŸ’¬ Support request
Status

Active

Version

11.0 πŸ”₯

Component
AjaxΒ  β†’

Last updated 1 day ago

Created by

πŸ‡ΊπŸ‡ΈUnited States seth.e.shaw

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Issue created by @seth.e.shaw
  • πŸ‡§πŸ‡ΎBelarus ---Runa---

    Hello.
    I think you already had the answer, but I put my version here, because I found this page when was resolving a similar issue.
    Drupal.ajax(ajax_settings).execute();
    It executes the request in the same moment as it called. The only problem here is as the callback has been executed, the behaviour attached again and all ajax calls executed again. So it should be something like.

    if ($(.selector.ajax-executed).length == 0) {  
    Drupal.ajax(ajax_settings).execute().done($('.selector').addClass('ajax-executed'));
    }
    
Production build 0.71.5 2024