From a855c9c6265347a647924155c1f8d2a05c18b6a9 Mon Sep 17 00:00:00 2001 From: "Hemanth.HM" Date: Sat, 25 May 2013 13:14:46 +0530 Subject: [PATCH 1/4] Trun off event(s) --- src/$.js | 134 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 85 insertions(+), 49 deletions(-) diff --git a/src/$.js b/src/$.js index 84a5a29..6e05d21 100644 --- a/src/$.js +++ b/src/$.js @@ -1,51 +1,87 @@ /*globals Element:true, NodeList:true*/ -$ = (function (document, $) { - var element = Element.prototype, - nodeList = NodeList.prototype, - forEach = 'forEach', - trigger = 'trigger', - each = [][forEach], - dummy = document.createElement(); - - nodeList[forEach] = each; - - element.on = function (event, fn) { - this.addEventListener(event, fn, false); - return this; - }; - - nodeList.on = function (event, fn) { - each.call(this, function (el) { - el.on(event, fn); - }); - return this; - }; - - element.trigger = function (type, data) { - var event = document.createEvent('HTMLEvents'); - event.initEvent(type, true, true); - event.data = data || {}; - event.eventName = type; - event.target = this; - this.dispatchEvent(event); - return this; - }; - - nodeList.trigger = function (event) { - each.call(this, function (el) { - el[trigger](event); - }); - return this; - }; - - $ = function (s) { - var r = document.querySelectorAll(s || '☺'), - length = r.length; - return length == 1 ? r[0] : !length ? nodeList : r; - }; - - $.on = element.on.bind(dummy); - $.trigger = element[trigger].bind(dummy); - - return $; +$ = (function(document, $) { + var element = Element.prototype, + nodeList = NodeList.prototype, + forEach = 'forEach', + trigger = 'trigger', + each = [][forEach], + dummy = document.createElement(); + + nodeList[forEach] = each; + + element.on = function(event, fn) { + if (!this.handlers) { + this.handlers = [] + } + this.addEventListener(event, fn, false); + this.handlers.push(fn); + return this; + }; + + element.off = function(event, fn) { + if (!fn) { + var self = this; + this.handlers.forEach(function(handler) { + self.removeEventListener(event, handler, false); + }); + } else { + this.removeEventListener(event, fn, false); + } + return this; + }; + + nodeList.on = function(event, fn) { + if (!this.handlers) { + this.handlers = [] + } + each.call(this, function(el) { + el.on(event, fn); + this.handlers.push(fn); + }); + return this; + }; + + nodeList.off = function(event, fn) { + if(!fn){ + var self = this; + this.handlers.forEach(function(handler) { + self.off(event, handler, false); + }); + } + else{ + each.call(this, function(el) { + el.off(event, fn); + }); + } + return this; + }; + + element.trigger = function(type, data) { + var event = document.createEvent('HTMLEvents'); + event.initEvent(type, true, true); + event.data = data || {}; + event.eventName = type; + event.target = this; + this.dispatchEvent(event); + return this; + }; + + nodeList.trigger = function(event) { + each.call(this, function(el) { + el[trigger](event); + }); + return this; + }; + + $ = function(s) { + var r = document.querySelectorAll(s || '☺'), + length = r.length; + return length == 1 ? r[0] : !length ? nodeList : r; + }; + + $.on = element.on.bind(dummy); + $.off = element.off.bind(dummy); + $.trigger = element[trigger].bind(dummy); + + return $; })(document); From 9dc39357d781b8a4d92b3a4de8e8fe90dc76ec86 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Sat, 25 May 2013 15:59:12 +0530 Subject: [PATCH 2/4] Made it merge compliant. --- src/$.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/$.js b/src/$.js index 6e05d21..f37a566 100644 --- a/src/$.js +++ b/src/$.js @@ -20,9 +20,8 @@ $ = (function(document, $) { element.off = function(event, fn) { if (!fn) { - var self = this; - this.handlers.forEach(function(handler) { - self.removeEventListener(event, handler, false); + this.handlers[forEach](function(handler) { + this.removeEventListener(event, handler, false); }); } else { this.removeEventListener(event, fn, false); @@ -31,9 +30,6 @@ $ = (function(document, $) { }; nodeList.on = function(event, fn) { - if (!this.handlers) { - this.handlers = [] - } each.call(this, function(el) { el.on(event, fn); this.handlers.push(fn); @@ -42,13 +38,12 @@ $ = (function(document, $) { }; nodeList.off = function(event, fn) { - if(!fn){ - var self = this; - this.handlers.forEach(function(handler) { - self.off(event, handler, false); + if(!fn) { + this.handlers[forEach](function(handler) { + this.off(event, handler, false); }); } - else{ + else { each.call(this, function(el) { el.off(event, fn); }); From f5d15f889bee668e1b5994de56f9283a0d180127 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Sat, 25 May 2013 16:07:46 +0530 Subject: [PATCH 3/4] Fixed white spaces and hugging braces. --- src/$.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/$.js b/src/$.js index f37a566..50aa03d 100644 --- a/src/$.js +++ b/src/$.js @@ -23,7 +23,7 @@ $ = (function(document, $) { this.handlers[forEach](function(handler) { this.removeEventListener(event, handler, false); }); - } else { + }else { this.removeEventListener(event, fn, false); } return this; @@ -42,8 +42,7 @@ $ = (function(document, $) { this.handlers[forEach](function(handler) { this.off(event, handler, false); }); - } - else { + }else { each.call(this, function(el) { el.off(event, fn); }); From 0b641bb536c0003c051ec4004bf212f70182e1c7 Mon Sep 17 00:00:00 2001 From: "Hemanth.HM" Date: Sun, 26 May 2013 07:48:09 +0530 Subject: [PATCH 4/4] Avoding self and binding this --- src/$.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/$.js b/src/$.js index 50aa03d..263186f 100644 --- a/src/$.js +++ b/src/$.js @@ -22,7 +22,7 @@ $ = (function(document, $) { if (!fn) { this.handlers[forEach](function(handler) { this.removeEventListener(event, handler, false); - }); + }.bind(this)); }else { this.removeEventListener(event, fn, false); } @@ -41,7 +41,7 @@ $ = (function(document, $) { if(!fn) { this.handlers[forEach](function(handler) { this.off(event, handler, false); - }); + }.bind(this)); }else { each.call(this, function(el) { el.off(event, fn);