From a2cc04a8b3d225f7c84d09ab9323ca2cd4b02628 Mon Sep 17 00:00:00 2001 From: Emanuele Rampichini Date: Tue, 25 Mar 2014 10:24:50 +0100 Subject: [PATCH 1/2] Plugin is now destroyable. --- bootstrap-touchspin/bootstrap.touchspin.js | 32 ++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/bootstrap-touchspin/bootstrap.touchspin.js b/bootstrap-touchspin/bootstrap.touchspin.js index bbde86d..4d74e8c 100644 --- a/bootstrap-touchspin/bootstrap.touchspin.js +++ b/bootstrap-touchspin/bootstrap.touchspin.js @@ -33,8 +33,36 @@ (function($) { "use strict"; + var _currentSpinnerId = 0; + + function _scopedEventName(name, id) { + return name + ".touchspin_" + id; + } + + function _scopeEventNames(names, id) { + return $.map(names, function(name) { + return _scopedEventName(name, id); + }); + } + $.fn.TouchSpin = function(options) { + if (options === "destroy") { + this.each(function() { + var originalinput = $(this), + originalinput_data = originalinput.data(); + $(document).off(_scopeEventNames([ + "mouseup", + "touchend", + "touchcancel", + "mousemove", + "touchmove", + "scroll", + "scrollstart"], originalinput_data.spinnerid).join(" ")); + }); + return; + } + var defaults = { min: 0, max: 100, @@ -334,7 +362,7 @@ ev.preventDefault(); }); - $(document).on("mouseup touchend touchcancel", function(ev) { + $(document).on(_scopeEventNames(["mouseup", "touchend", "touchcancel"], _currentSpinnerId).join(" "), function(ev) { if (!spinning) { return; } @@ -343,7 +371,7 @@ stopSpin(); }); - $(document).on("mousemove touchmove scroll scrollstart", function(ev) { + $(document).on(_scopeEventNames(["mousemove", "touchmove", "scroll", "scrollstart"], _currentSpinnerId).join(" "), function(ev) { if (!spinning) { return; } From 1b8de050026cdefdbdf96212de907361ba078eaa Mon Sep 17 00:00:00 2001 From: Emanuele Rampichini Date: Tue, 25 Mar 2014 10:42:12 +0100 Subject: [PATCH 2/2] Plugin is now destroyable. --- bootstrap-touchspin/bootstrap.touchspin.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bootstrap-touchspin/bootstrap.touchspin.js b/bootstrap-touchspin/bootstrap.touchspin.js index 4d74e8c..ea21754 100644 --- a/bootstrap-touchspin/bootstrap.touchspin.js +++ b/bootstrap-touchspin/bootstrap.touchspin.js @@ -100,6 +100,7 @@ spinning = false; init(); + function init() { if (originalinput.data("alreadyinitialized")) { @@ -107,6 +108,9 @@ } originalinput.data("alreadyinitialized", true); + _currentSpinnerId +=1; + originalinput.data("spinnerid", _currentSpinnerId); + if (!originalinput.is("input")) { console.log("Must be an input.");