Drupal 行为 [英] Drupal behaviors

查看:28
本文介绍了Drupal 行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • Drupal 的行为到底是什么?
  • 它为模块开发人员提供什么类型的服务层?
  • 它映射到 jQuery.ready 的关系类型是什么?
  • 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?

推荐答案

长版:Drupal.behaviors 不仅仅是 jQuery.ready 的替代品,因为后者只运行一次(当 DOM 就绪时用于操作):行为可以在页面执行期间多次触发,并且可以在新的 DOM 元素插入文档时运行.

Long version: Drupal.behaviors is not simply a replacement for jQuery.ready since the latter only runs once (when DOM is ready for manipulation): behaviors can be fired multiple times during page execution and can be run whenever new DOM elements are inserted into the document.

此外,模块可以覆盖或扩展现有行为(例如,如果一个模块具有在所有链接上添加反弹效果的行为,则第二个模块可以用不同的反弹效果替换该行为).

Also, modules could override or extend an existing behavior (e.g. if one module has a behavior of adding a bounce effect on all links, a second module could replace the behavior by a different bounce effect).

简短版本:虽然文档可以改进,但更加模块化.

Short version: it's more modular, though the documentation could be improved.

此外,从 Drupal 7 开始,使用 drupal_add_js (PHP) 或在 Drupal.settings.modulename (Javascript) 中定义的设置直接作为第二个参数(第一个一个是上下文)到行为.

Also, starting in Drupal 7, settings defined using drupal_add_js (PHP) or in Drupal.settings.modulename (Javascript) are directly passed as second parameter (the first one being the context) to the behavior.

例如:

Drupal.behaviors.changeLinks = function(context, settings){
    if (!settings) settings = Drupal.settings.changeLinks;
    $("a", context).hover(function() {
        $(this).css('color', settings.color);
    });
};

如果您的一个脚本(或另一个)创建了新节点,它仍然可以将行为应用于新节点,而无需知道安装了哪些其他模块:

And if one of your script (or another) creates new nodes, it could still have the behaviors applied to the new nodes without having to know what other modules are iinstalled:

var newNodes = $('<a href="#">Hello</a> <a href="#">World</a>').appendTo('#someDiv');

Drupal.attachBehaviors(newNodes);

这篇关于Drupal 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆