Liens utiles:
- Doc officielle: Managing JavaScript in Drupal 7 – Drupal 7 has introduced several new techniques that allow you far greater flexibility and control in the scripts you can have on your Drupal site’s pages.
- Understanding JavaScript behaviors in Drupal – I can barely remember the first time I added JavaScript to a Drupal page using a custom module. I’m sure looked at the documentation, took the example snippet, tweaked it, tested it, and moved on to something else. It was only later, using a debugger, that I saw how Drupal’s « behavior » system worked and realized that my code was not being executed as I expected. In this article, we’ll cover the key facts about Drupal behaviors, then look at a real Drupal project to inspect its behaviors and optimize them.
Les JavaScript behaviors dans Drupal 7
Ressources en français : Drupal behaviors en Drupal 7 – Les Drupal behaviors permettent de déclencher des actions jQuery. Leur force réside dans le fait que, en cas de modification du DOM, Drupal va automatiquement les rappeler et permettre ainsi aux différentes actions de se lier sur les nouveaux éléments du DOM.
Avant:
1 2 3 4 5 |
$(document).ready(function(){ $('#search input:text').autofill({ value: "Search..." }); }); |
Après:
1 2 3 4 5 6 7 8 9 |
;(function($){ Drupal.behaviors.ninesixtyrobots = { attach: function(context) { $('#search-block-form input:text', context).autofill({ value: "Search..." }); } }; })(jQuery); |