Запуск behaviors один раз

Главные вкладки

Лучший ответ

Аватар пользователя VVS VVS 26 января 2021 в 0:35

Нагуглил решение:

Запускаем скрипт только, когда context равен document.

Drupal.behaviors.dosomething = {
  attach: function attach(context, settings) {
    if (context !== document) {
        return;
    }
    // Do stuff here but still use .once when attaching behaviors.
  ...

Комментарии

Аватар пользователя VVS VVS 26 января 2021 в 0:35

Нагуглил решение:

Запускаем скрипт только, когда context равен document.

Drupal.behaviors.dosomething = {
  attach: function attach(context, settings) {
    if (context !== document) {
        return;
    }
    // Do stuff here but still use .once when attaching behaviors.
  ...
Аватар пользователя OldWarrior OldWarrior 26 января 2021 в 5:41

VVS wrote: Как once в jQuery.

Как-то так:

(function ($, Drupal) {
  Drupal.behaviors.dosomething = {
    attach: function (context, settings) {
      $(document, context).once('myCustomBehavior').each(function () {
        // Do stuff here...
      });
    }
  };
})(jQuery, Drupal);

Также в .MODULE.libraries.yml :

...
  dependencies:
    - core/jquery
    - core/jquery.once
...

Подробнее: https://www.drupal.org/docs/drupal-apis/javascript-api/javascript-api-ov...