Drupal behaviors

后端 未结 6 1934
南方客
南方客 2020-12-12 12:42
  • What are Drupal behaviors at all?
  • What type of service layer it offers to module developers?
  • What type of relation it maps to jQuery.ready
6条回答
  •  失恋的感觉
    2020-12-12 13:12

    Drupal behaviors are used if your JavaScript needs to be executed on page load and after an AJAX request (some new elements added in your document/html).

    Drupal.behaviors.yourmodulename = {
      attach: function (context, settings) {
         // Code to be run on page load, and
        // on ajax load added here
      }
    };
    

    yourmodulename: This is your namespace and should be unique. For example, this is typically the name of your module, but it isn't mandatory.

    context: This is actually really really cool, on page load the context will contain the entire document and after an AJAX request will have all the newly loaded elements. This way you can treat content that is loaded in via AJAX differently than others.

    settings: This contains information passed on to JavaScript via PHP, it is similar to accessing it via Drupal.settings.

提交回复
热议问题