UFO ET IT

Drupal 행동

ufoet 2020. 12. 30. 08:32
반응형

Drupal 행동


  • Drupal 행동이란 무엇입니까?
  • 모듈 개발자에게 어떤 유형의 서비스 계층을 제공합니까?
  • 어떤 유형의 관계에 매핑 jQuery.ready됩니까?

긴 버전 : Drupal.behaviors는 jQuery.ready를 단순히 대체하는 것이 아닙니다. 후자는 한 번만 실행되기 때문입니다 (DOM이 조작 할 준비가 된 경우) : 동작은 페이지 실행 중에 여러 번 실행될 수 있으며 새 DOM 요소가 삽입 될 때마다 실행될 수 있습니다. 문서.

또한 모듈은 기존 동작을 재정의하거나 확장 할 수 있습니다 (예 : 한 모듈에 모든 링크에 바운스 효과를 추가하는 동작이있는 경우 두 번째 모듈이 동작을 다른 바운스 효과로 대체 할 수 있음).

짧은 버전 : 문서가 개선 될 수 있지만 더 모듈 식입니다.


또한 Drupal 7부터 drupal_add_js (PHP) 또는 Drupal.settings.modulename (Javascript)을 사용하여 정의 된 설정 은 두 번째 매개 변수 (첫 번째는 컨텍스트)로 동작에 직접 전달됩니다.

예를 들면 :

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

스크립트 중 하나 (또는 ​​다른 스크립트)가 새 노드를 생성하는 경우 다른 모듈이 iinstall되어 있는지 알 필요없이 새 노드에 동작을 적용 할 수 있습니다.

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

Drupal.attachBehaviors(newNodes);

중복 된 기능

Drupal.behaviors 아키텍처는 이미 jQuery에있는 기능을 복제합니다 .

또한이 글을 쓰는 시점에서 Drupal 자체 외부의 Drupal.behaviors에 대한 문서 나 사례 연구는없는 것으로 보입니다 . 그리고 Drupal 내의 문서 (위에서 언급했듯이)는 개선을 통해 상당한 이점을 얻을 수 있습니다. 이 글을 쓰는 시점에서 기본 상세 문서는 유료로만 액세스가 제한된 것으로 보입니다.

, Drupal.behaviors 에코 시스템에 고유 한 표준 jQuery와 일치하지 않는 성능 저하, 이상 및 예기치 않은 결과를 발견 할 수 있습니다 .

기본 jQuery 기능

Drupal.behaviors와 달리 표준 jQuery API의 기본 제공 기능은 인라인 데모 및 예제를 포함하여 광범위하게 문서화 되어 있습니다 . 또한 jsfiddle과 같은 사이트에서 무료로 사용할 수있는 수많은 라이브 예제가 있습니다.

See also 섹션의 링크는 문서에 삽입 된 새 DOM 요소 처리와 관련된 jQuery API 호출을 열거합니다.

또한보십시오


위에서 언급 한 주요 사항에 대한 답변과 함께 다음과 같이 php에서 javascript로 데이터를 전달할 수 있습니다.

"Drupal.settings"를 사용하여 PHP에서 Javascript로 값 전달

drupal_add_js () 함수를 사용하여 Drupal.settings를 사용하여 PHP의 변수를 프런트 엔드의 Javascript에서 쉽게 사용할 수 있습니다.

<?php
  drupal_add_js(array('myModule' => array('key' => 'value')), 'setting');
?>

또는

<?php
$element['#attached']['js'][] = array(
  'type' => 'setting',
  'data' => array('myModule' => array('key' => 'value')),
);
?>

이것은 Javascript에서 다음과 같이 사용할 수 있습니다.

  if (Drupal.settings.myModule.key === 'value') {
    alert('Got it!');
  }

비슷한 대답을 찾고 여기에 도착했지만 여전히 단서가 없습니다. 마지막으로 https://benmarshall.me/drupal-behaviors/ 기사에서 좀 더 많은 설명 (및 예제)을 찾았습니다.

저는 원저자가 아니기 때문에 일부 텍스트 만 인용 할 수 있습니다.

Drupal 행동이란 무엇입니까?

요컨대, Drupal.behaviors는 jQuery.ready를 구현하는 더 모듈화되고 더 나은 방법입니다. DOM이 조작 할 준비가되었을 때 한 번만 실행되는 jQuery.ready와 달리 Drupal.behaviors는 페이지 실행 중에 여러 번 실행할 수 있습니다. 더 좋은 점은 새 DOM 요소가 문서에 삽입 될 때마다 실행될 수 있다는 것입니다 (예 : AJAX 기반 콘텐츠).

Drupal.behaviors can also override or even extend an existing behavior. So for instance, if a module behavior adds a bounce effect on all links, another module could replace that behavior with a different bounce effect.

Another added bonus of Drupal.behaviors (starting in Drupal 7), is the ability to use the drupal_add_js (PHP) or Drupal.settings.modulename (JS) and pass settings as a second parameter (the first being the context) to the behavior.


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.


Drupal has a ‘behaviors’ system to provide a modular and better way for attaching JavaScript functionality to place elements on a page. Drupal Behaviors allows you to override or extend the existing behavior. These Drupal behaviors are event triggered programs that get attached to the page elements to be changed. While behaviours can be attached to specific contents, multiple behaviours are also attached and can be ablazed multiple times for a quick remake.

JavaScript by attaching logic to Drupal.behaviors. Here is an example taken from that page:

Drupal.behaviors.exampleModule = {
  attach: function (context, settings) {
    $('.example', context).click(function () {
      $(this).next('ul').toggle('show');
    });
  }
}

;

ReferenceURL : https://stackoverflow.com/questions/3941426/drupal-behaviors

반응형