delegate.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.NodeList.delegate"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.NodeList.delegate"] = true;
  8. dojo.provide("dojox.NodeList.delegate");
  9. dojo.require("dojo.NodeList-traverse");
  10. dojo.extend(dojo.NodeList, {
  11. delegate: function(/*String*/ selector, /*String*/ eventName, /*Function*/ fn){
  12. // summary:
  13. // Monitor nodes in this NodeList for [bubbled] events on nodes that match selector.
  14. // Calls fn(evt) for those events, where (inside of fn()), this == the node
  15. // that matches the selector.
  16. // description:
  17. // Sets up event handlers that can catch events on any subnodes matching a given selector,
  18. // including nodes created after delegate() has been called.
  19. //
  20. // This allows an app to setup a single event handler on a high level node, rather than many
  21. // event handlers on subnodes. For example, one onclick handler for a Tree widget, rather than separate
  22. // handlers for each node in the tree.
  23. // Since setting up many event handlers is expensive, this can increase performance.
  24. //
  25. // Note that delegate() will not work for events that don't bubble, like focus.
  26. // onmouseenter/onmouseleave also don't currently work.
  27. // selector:
  28. // CSS selector valid to `dojo.query`, like ".foo" or "div > span". The
  29. // selector is relative to the nodes in this NodeList, not the document root.
  30. // For example myNodeList.delegate("> a", "onclick", ...) will catch events on
  31. // anchor nodes which are (immediate) children of the nodes in myNodeList.
  32. // eventName:
  33. // Standard event name used as an argument to `dojo.connect`, like "onclick".
  34. // fn:
  35. // Callback function passed the event object, and where this == the node that matches the selector.
  36. // That means that for example, after setting up a handler via
  37. // dojo.query("body").delegate("fieldset", "onclick", ...)
  38. // clicking on a fieldset or *any nodes inside of a fieldset* will be reported
  39. // as a click on the fieldset itself.
  40. // example:
  41. // | dojo.query("navbar").delegate("a", "onclick", function(evt){
  42. // | console.log("user clicked anchor ", this.node);
  43. // | });
  44. // Possible future tasks:
  45. // - change signature of callback to be fn(node, evt), and then have scope argument
  46. // to delegate(selector, eventName, scope, fn)?
  47. // - support non-bubbling events like focus
  48. // - support onmouseenter/onmouseleave
  49. // - maybe should return an array of connect handles instead, to allow undelegate()?
  50. // - single node version
  51. return this.connect(eventName, function(evt){
  52. var closest = dojo.query(evt.target).closest(selector, this);
  53. if(closest.length){
  54. fn.call(closest[0], evt);
  55. }
  56. }); //dojo.NodeList
  57. }
  58. });
  59. }