diff --git a/jquery.autosize.input.js b/jquery.autosize.input.js index d4bf181..40b47e7 100644 --- a/jquery.autosize.input.js +++ b/jquery.autosize.input.js @@ -14,6 +14,9 @@ var Plugins; var _this = this; this._input = $(input); this._options = $.extend({}, AutosizeInput.getDefaultOptions(), options); + this._updateHandler = function(e){ + _this.update() + }; // Init mirror this._mirror = $(''); @@ -30,9 +33,7 @@ var Plugins; // How to fix problem with hitting the delete "X" in the box - but not updating!? mouseup is apparently to early // Could bind separatly and set timer // Add so it automatically updates if value of input is changed http://stackoverflow.com/a/1848414/58524 - this._input.on("keydown keyup input propertychange change", function (e) { - _this.update(); - }); + this._input.on("keydown keyup input propertychange change", this._updateHandler); // Update (function () { @@ -43,6 +44,11 @@ var Plugins; return this._options; }; + AutosizeInput.prototype.destroy = function() { + this._mirror.remove(); + this._input.off("keydown keyup input propertychange change", null, this._updateHandler); + }; + AutosizeInput.prototype.update = function () { var value = this._input.val() || ""; @@ -78,9 +84,15 @@ var Plugins; (function ($) { var pluginDataAttributeName = "autosize-input"; var validTypes = ["text", "password", "search", "url", "tel", "email", "number"]; + var methods = { + destroy : function() { + var $this = $(this); + $this.data(Plugins.AutosizeInput.getInstanceKey()).destroy() + } + }; // jQuery Plugin - $.fn.autosizeInput = function (options) { + $.fn.autosizeInput = function (optionsAndMethods) { return this.each(function () { // Make sure it is only applied to input elements of valid type // Or let it be the responsibility of the programmer to only select and apply to valid elements? @@ -91,16 +103,28 @@ var Plugins; var $this = $(this); + if ( options == undefined ) { + if (methods[optionsAndMethods]){ + var method = optionsAndMethods; + }else{ + var options = optionsAndMethods; + } + } + if (!$this.data(Plugins.AutosizeInput.getInstanceKey())) { // If instance not already created and attached if (options == undefined) { // Try get options from attribute - options = $this.data(pluginDataAttributeName); + var options = $this.data(pluginDataAttributeName); } // Create and attach instance $this.data(Plugins.AutosizeInput.getInstanceKey(), new Plugins.AutosizeInput(this, options)); } + + if ( method != undefined) { + return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); + } }); };