From 8861b1591168bbe7df7ea044a89b8ac21ac1669b Mon Sep 17 00:00:00 2001 From: Damian Krzeminski Date: Tue, 8 Apr 2014 08:53:03 -0400 Subject: [PATCH] improve binding of 'scroll' and 'resize' handlers ensure that 'scroll' and 'resize' handlers are in all cases bound at most once, and that they are always unbound when Tip is removed fixes #38 fixes #25 --- index.js | 14 +++++++++----- test/index.html | 9 +++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 63c6f11..0196ed3 100644 --- a/index.js +++ b/index.js @@ -59,7 +59,6 @@ function Tip(content, options) { this.delay = options.delay || 300; this.el = html.cloneNode(true); this.events = events(this.el, this); - this.winEvents = events(window, this); this.classes = classes(this.el); this.inner = query('.tip-inner', this.el); this.message(content); @@ -220,8 +219,11 @@ Tip.prototype.show = function(el){ this.reposition(); this.emit('show', this.target); - this.winEvents.bind('resize', 'reposition'); - this.winEvents.bind('scroll', 'reposition'); + if (!this.winEvents) { + this.winEvents = events(window, this); + this.winEvents.bind('resize', 'reposition'); + this.winEvents.bind('scroll', 'reposition'); + } return this; }; @@ -404,8 +406,10 @@ Tip.prototype.hide = function(ms){ */ Tip.prototype.remove = function(){ - this.winEvents.unbind('resize', 'reposition'); - this.winEvents.unbind('scroll', 'reposition'); + if (this.winEvents) { + this.winEvents.unbind(); + this.winEvents = null; + } this.emit('hide'); var parent = this.el.parentNode; diff --git a/test/index.html b/test/index.html index 1b00156..d9e6330 100644 --- a/test/index.html +++ b/test/index.html @@ -42,11 +42,17 @@ #border { border-width: 10px; } + .hide-tip { + margin-left: 4em; + } Tip +
+ +