API Docs for: 3.13.0
Show:

File: node/js/node-event-delegate.js

  1. /**
  2. * Functionality to make the node a delegated event container
  3. * @module node
  4. * @submodule node-event-delegate
  5. */
  6.  
  7. /**
  8. * <p>Sets up a delegation listener for an event occurring inside the Node.
  9. * The delegated event will be verified against a supplied selector or
  10. * filtering function to test if the event references at least one node that
  11. * should trigger the subscription callback.</p>
  12. *
  13. * <p>Selector string filters will trigger the callback if the event originated
  14. * from a node that matches it or is contained in a node that matches it.
  15. * Function filters are called for each Node up the parent axis to the
  16. * subscribing container node, and receive at each level the Node and the event
  17. * object. The function should return true (or a truthy value) if that Node
  18. * should trigger the subscription callback. Note, it is possible for filters
  19. * to match multiple Nodes for a single event. In this case, the delegate
  20. * callback will be executed for each matching Node.</p>
  21. *
  22. * <p>For each matching Node, the callback will be executed with its 'this'
  23. * object set to the Node matched by the filter (unless a specific context was
  24. * provided during subscription), and the provided event's
  25. * <code>currentTarget</code> will also be set to the matching Node. The
  26. * containing Node from which the subscription was originally made can be
  27. * referenced as <code>e.container</code>.
  28. *
  29. * @method delegate
  30. * @param type {String} the event type to delegate
  31. * @param fn {Function} the callback function to execute. This function
  32. * will be provided the event object for the delegated event.
  33. * @param spec {String|Function} a selector that must match the target of the
  34. * event or a function to test target and its parents for a match
  35. * @param context {Object} optional argument that specifies what 'this' refers to.
  36. * @param args* {any} 0..n additional arguments to pass on to the callback function.
  37. * These arguments will be added after the event object.
  38. * @return {EventHandle} the detach handle
  39. * @for Node
  40. */
  41. Y.Node.prototype.delegate = function(type) {
  42.  
  43. var args = Y.Array(arguments, 0, true),
  44. index = (Y.Lang.isObject(type) && !Y.Lang.isArray(type)) ? 1 : 2;
  45.  
  46. args.splice(index, 0, this._node);
  47.  
  48. return Y.delegate.apply(Y, args);
  49. };
  50.