Commit d2d79db2 authored by Bart Langelaan's avatar Bart Langelaan Committed by Geoff Appleby
Browse files

Issue #3214916: Behaviors will not load after AJAX call when using CSP Extras

parent 9482433e
Loading
Loading
Loading
Loading
+20 −0
Changes for csp_extras/js/ajax.js: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -18,6 +18,17 @@
   *   The XMLHttpRequest status.
   */
  Drupal.AjaxCommands.prototype.add_assets = function (ajax, response, status) {
    var assetsLoaded = 0;

    function onAssetLoad() {
      assetsLoaded += 1;

      // When new scripts are loaded, attach newly added behaviors.
      if (assetsLoaded >= response.assets.length) {
        Drupal.attachBehaviors(document.body, ajax.settings);
      }
    }

    response.assets.forEach(function (item) {
      var elem;
      var target = document.body;
@@ -37,6 +48,15 @@
        elem[key] = item.attributes[key];
      });

      if (item.type === "script") {
        elem.onload = onAssetLoad;
      }
      else {
        // Directly mark this element as loaded. We don't have to wait before
        // behaviours can be attached.
        onAssetLoad();
      }

      target.appendChild(elem);
    });
  };