From c7d86ee0e4698acc871735138241f4ca49de0384 Mon Sep 17 00:00:00 2001 From: Luis Leiva Date: Fri, 22 Aug 2014 17:15:21 +0200 Subject: [PATCH] Less intrusive init method for memento plugin --- jquery.sketchable.memento.js | 26 +++++++++++++------------- jquery.sketchable.memento.min.js | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/jquery.sketchable.memento.js b/jquery.sketchable.memento.js index 9b78149..f68c211 100644 --- a/jquery.sketchable.memento.js +++ b/jquery.sketchable.memento.js @@ -131,6 +131,8 @@ * @memberOf MementoCanvas */ this.init = function() { + // Prevent subsequent instances to re-attach this event to the document. + $(document).off("keypress", keyManager); $(document).on("keypress", keyManager); }; /** @@ -151,7 +153,9 @@ var availMethods = plugin('methods'); function configure(elem, opts) { - var options = $.extend(true, plugin.defaults, opts); + var self = elem, options = $.extend(true, plugin.defaults, opts); + // Actually this plugin is singleton, so exit early. + if (!options.interactive) return opts; var mc = new MementoCanvas(elem); @@ -174,9 +178,10 @@ if (options && options.events && typeof options.events[ev] === 'function') { var fn = options.events[ev]; options.events[ev] = function() { + // Exec original function first, then exec our callback. var args = Array.prototype.slice.call(arguments, 0); - fn.call(this, args); - callbacks[ev].apply(this, args); + fn.apply(self, args); + callbacks[ev].apply(self, args); } } else { plugin.defaults.events[ev] = callbacks[ev]; @@ -212,15 +217,10 @@ * $(selector).sketchable(); * $(selector).sketchable({interactive:false}); */ - $.fn.sketchable = function(method) { - if (typeof method === 'object' || !method) { - // Object creation: configure memento extension. - var newOptions = configure(this, arguments[0]); - return availMethods.init.apply(this, [newOptions]); - } else if (availMethods[method]) { - // Method invocation: execute it as usual. - return availMethods[method].apply(this, Array.prototype.slice.call(arguments, 1)); - } + var initfn = availMethods.init; + availMethods.init = function(opts) { + var conf = configure(this, opts); + return initfn.apply(this, [conf]); }; - + })(jQuery); diff --git a/jquery.sketchable.memento.min.js b/jquery.sketchable.memento.min.js index eb56210..fc9eacc 100644 --- a/jquery.sketchable.memento.min.js +++ b/jquery.sketchable.memento.min.js @@ -1,4 +1,4 @@ /*! * Memento plugin for jQuery sketchable | v1.1 | Luis A. Leiva | MIT license */ -;(function(d){var a=function(l){var g=[];var f=-1;var h=this;function k(){if(f>0){f--;var n=new Image();n.src=g[f].image;n.onload=function(){i(this)}}}function j(){if(f0){g--;var o=new Image();o.src=h[g].image;o.onload=function(){j(this)}}}function k(){if(g