Permalink
Browse files

Added jquery.on.off.js

  • Loading branch information...
MoonScript committed Sep 4, 2012
1 parent a7930f3 commit 1ec7fe2d59ec11ba6acb9d8ca7763959c2522cf0
Showing with 43 additions and 0 deletions.
  1. +43 −0 jquery.on.off.js
View
@@ -0,0 +1,43 @@
+(function($){
+"use strict";
+
+var undelegateOrUnbind = function(events, selector, handler){
+ return ($.type(selector) !== 'string') ? this.unbind(events, handler) : this.undelegate(selector, events, handler);
+};
+
+// Stub in "on/off" for jQuery >= 1.4.3 and < 1.7
+if (!$.fn.on && $.fn.delegate && ($.fn.jquery !== '1.4.2')) {
+ $.fn.on = function(events, selector, data, handler){
+ var $collection = this;
+ var useBind = $.type(selector) !== 'string';
+ // Handle either a string of event names or a map of events-and-callbacks for the 1st parameter
+ if ($.isPlainObject(events)) {
+ if (useBind) {
+ return $collection.bind(events);
+ }
+ if ($.isPlainObject(data)) {
+ $.each(events, function(eventName, callback){
+ $collection.delegate(selector, eventName, data, callback);
+ });
+ return $collection;
+ }
+ return $collection.delegate(selector, events);
+ }
+ if (useBind) {
+ return $.fn.bind.apply($collection, arguments);
+ }
+ return $collection.delegate(selector, events, data, handler);
+ };
+ $.fn.off = function(events, selector, handler){
+ var $collection = this;
+ if ($.isPlainObject(events)) {
+ $.each(events, function(eventName, callback){
+ undelegateOrUnbind.apply($collection, [eventName, selector, callback]);
+ });
+ return $collection;
+ }
+ return undelegateOrUnbind.apply($collection, arguments);
+ };
+}
+
+})(jQuery);

0 comments on commit 1ec7fe2

Please sign in to comment.