From 34503daccf1afe28d40e1fd3057e91450b347163 Mon Sep 17 00:00:00 2001 From: David Neilsen Date: Wed, 6 Jan 2016 11:09:29 +1300 Subject: [PATCH] Add destroy method to remove events bound to window --- jquery.fittext.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/jquery.fittext.js b/jquery.fittext.js index 0b3ddef..7858fe7 100644 --- a/jquery.fittext.js +++ b/jquery.fittext.js @@ -12,6 +12,15 @@ (function( $ ){ $.fn.fitText = function( kompressor, options ) { + if (kompressor === 'destroy') { + return this.each(function(){ + var uid = $(this).data('fitText.uid'); + if (uid) { + $(window).off('.' + uid); + } + }); + return; + } // Setup options var compressor = kompressor || 1, @@ -32,9 +41,13 @@ // Call once to set. resizer(); + + // Create an event unique ID to allow unbinding + var uid = Math.random().toString().replace(/^[0\.]+/, 'uid-'); + $this.data('fitText.uid', uid); // Call on resize. Opera debounces their resize by default. - $(window).on('resize.fittext orientationchange.fittext', resizer); + $(window).on('resize.fittext.' + uid + ' orientationchange.fittext.' + uid, resizer); });