Don't like ads? PRO users don't see any ads ;-)
Guest

jQuery event priority

By: Dramiel on Dec 7th, 2011  |  syntax: JavaScript  |  size: 1.54 KB  |  hits: 90  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // extend jquery to allow event priority
  2. (function ($) {
  3.         var _on = $.fn.on;
  4.  
  5.         function enumTypes(types, fn) {
  6.                 types = jQuery.trim(types).split(" ");
  7.                 for (i = 0; i < types.length; i++) {
  8.                         var type = types[i];
  9.  
  10.                         fn.call(this, type);
  11.                 }
  12.         }
  13.  
  14.         function sortEvents(types) {
  15.                 enumTypes.call(this, types, function (type) {
  16.  
  17.                         this.each(function () {
  18.                                 var events = $.data(this, "events")[type.split(".")[0]];
  19.  
  20.                                 var handlers = events.slice(events.delegateCount);
  21.                                 handlers.sort(function (a, b) {
  22.                                         var ap = typeof (a.priority) === "number" ? a.priority : 0;
  23.                                         var bp = typeof (b.priority) === "number" ? b.priority : 0;
  24.  
  25.                                         if (ap > bp)
  26.                                                 return -1;
  27.  
  28.                                         if (ap < bp)
  29.                                                 return 1;
  30.  
  31.                                         return 0;
  32.                                 });
  33.  
  34.                                 for (var i = 0; i < handlers.length; i++) {
  35.                                         events[events.delegateCount + i] = handlers[i];
  36.                                 }
  37.                         });
  38.  
  39.                 });
  40.         }
  41.  
  42.         $.fn.on = function (types, selector, data, fn) {
  43.                 if (typeof types === "object") {
  44.                         return _on.apply(this, arguments);
  45.                 }
  46.  
  47.                 var result;
  48.  
  49.                 if (typeof types === "string" && typeof selector === "number" && typeof data === "function") {
  50.                         var priority = selector;
  51.                         fn = data;
  52.  
  53.                         result = _on.call(this, types, fn);
  54.  
  55.                         enumTypes.call(this, types, function (type) {
  56.  
  57.                                 this.each(function () {
  58.                                         var events = $.data(this, "events")[type.split('.')[0]];
  59.  
  60.                                         events[events.length - 1].priority = priority;
  61.                                 });
  62.  
  63.                         });
  64.                 } else
  65.                         result = _on.apply(this, arguments);
  66.  
  67.                 sortEvents.call(this, types);
  68.  
  69.                 return result;
  70.         }
  71. } (jQuery));
  72.