Drupal.behave
Categories
Component ID
2481173
Component name
Drupal.behave
Component type
module
Maintenance status
Development status
Component security advisory coverage
covered
Downloads
1186
Component created
Component changed
Component body
Super sexy Drupal JS behaviors.
API in a nutshell
Drupal.behave(...)
It's a super simple, jQuery like, chainable API.
Before: The Conventional Drupal 7 Way
(function ($) { Drupal.behaviors.exampleModule = { attach: function (context, settings) { $('.myDOM', context).text('Who throws a shoe?!?'); } }; })(jQuery);
After: Yeah, Baby Way!
jQuery is also passed in as the third argument. Yeah, baby!
Drupal.behave('exampleModule').attach(function (context, settings, $) { $('.myDOM', context).text('Who throws a shoe?!?'); });
Even easier, you can use .ready without the context and settings arguments — the function context (this) will provide context, settings1, and behavior2. Easier to read and write.
Drupal.behave('exampleModule').ready(function ($) { $('.myDOM', this.context).text('Who throws a shoe?!?'); });
1The settings property on the function context refers to Drupal.settings.
2The behavior property on the function context refers to Drupal.behaviors.exampleModule in this case.
Detach, if you want.
Drupal.behave('exampleModule') .attach(function (context, settings, $) { $('.myDOM', context).text('Who throws a shoe?!?'); }); .detach(function (context, settings, trigger, $) { $('.myDOM', context).text('Oh, behave!'); })
Extending the Drupal behavior
You can extend the Drupal behavior, e.g., with your own functions, like this:
Drupal.behave('exampleModule') .extend({ myFunction: function myFunction() { } });
Low-level access to the behave object
If you really want to, you can grab the behave object.
var behave = Drupal.behave('exampleModule').behave();
