You are here

README.txt in Behavior weights 6

Same filename and directory in other branches
  1. 7 README.txt
ABOUT
  This is an API module.
  Only enable it if another module requires it, or if you want to use the API
  with your own custom code.

INSTALLATION
  Enable, that's it.

API USAGE
  In your own module's javascript, you can write something like this:

(function(){
  // at the moment this script runs, we do not know if behavior_weights.js has
  // already run or not. Lucky for us, this does not matter.

  // Let this run before other behaviors
  Drupal.behaviors['mymodule_early.weight'] = -10;

  // Let this run after other behaviors
  Drupal.behaviors['mymodule_late.weight'] = 10;

  Drupal.behaviors.mymodule_early = function(context){
    .. // your stuff to happen.
  };

  Drupal.behaviors.mymodule_late = function(context){
    .. // your stuff to happen.
  };
})();

  The default weight is 0. Anything with a smaller weight will run earlier.
  Anything with a higher weight will run later.

  A note about order of javascript object properties:
  In the javascript spec, there is no guarantee that a "for (var k in x)" loop
  will iterate the attributes of x in the same sequence as they were attached.
  Luckily, almost (?) all browsers are conservative enough, so we don't have to
  worry about this.
  The module tries to behave like a stable sort algorithm: Behaviors with the
  same weight should retain their order.
  If you ever see this fail, you should post a bug report!

  A note on Drupal.behaviors:
  If you want your behavior to run only once on page load, you need to check if
  the context element is the document root.
  For instance, check for $('body', context).length, or wrap your script inside
  $('body', context).each(function(){...});

File

README.txt
View source
  1. ABOUT
  2. This is an API module.
  3. Only enable it if another module requires it, or if you want to use the API
  4. with your own custom code.
  5. INSTALLATION
  6. Enable, that's it.
  7. API USAGE
  8. In your own module's javascript, you can write something like this:
  9. (function(){
  10. // at the moment this script runs, we do not know if behavior_weights.js has
  11. // already run or not. Lucky for us, this does not matter.
  12. // Let this run before other behaviors
  13. Drupal.behaviors['mymodule_early.weight'] = -10;
  14. // Let this run after other behaviors
  15. Drupal.behaviors['mymodule_late.weight'] = 10;
  16. Drupal.behaviors.mymodule_early = function(context){
  17. .. // your stuff to happen.
  18. };
  19. Drupal.behaviors.mymodule_late = function(context){
  20. .. // your stuff to happen.
  21. };
  22. })();
  23. The default weight is 0. Anything with a smaller weight will run earlier.
  24. Anything with a higher weight will run later.
  25. A note about order of javascript object properties:
  26. In the javascript spec, there is no guarantee that a "for (var k in x)" loop
  27. will iterate the attributes of x in the same sequence as they were attached.
  28. Luckily, almost (?) all browsers are conservative enough, so we don't have to
  29. worry about this.
  30. The module tries to behave like a stable sort algorithm: Behaviors with the
  31. same weight should retain their order.
  32. If you ever see this fail, you should post a bug report!
  33. A note on Drupal.behaviors:
  34. If you want your behavior to run only once on page load, you need to check if
  35. the context element is the document root.
  36. For instance, check for $('body', context).length, or wrap your script inside
  37. $('body', context).each(function(){...});