From 8dd3a3e56b67cb50998ad430f53bb1d161d7f84b Mon Sep 17 00:00:00 2001 From: Ben Taylor Date: Thu, 22 Mar 2012 12:13:06 +1100 Subject: [PATCH 1/2] Update js/jquery.precedent.js to add functionality for Cleaning up * Adds $.precedentCleanUp and $(selector).precedentCleanUp to clean up all, or individual precedents * Places a placeholders array in scope of the wrapping closure --- js/jquery.precedent.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/js/jquery.precedent.js b/js/jquery.precedent.js index 927a2b4..5c5c905 100644 --- a/js/jquery.precedent.js +++ b/js/jquery.precedent.js @@ -13,6 +13,7 @@ */ (function($) { + // This represents an object "placeholder" var Placeholder = function($element, options){ @@ -225,13 +226,51 @@ }; + Placeholder.prototype.cleanUp = function(){ + //Do Cleaning tasks here + }; + + var placeholders = []; + $.fn.precedent = function(options){ // Iterate over all the elements and initialise placeholder for them. this.filter('input, textarea').each(function() { - var placeholder = new Placeholder($(this), options); + precendented_elements.push(new Placeholder($(this), options)); }); return this; //Don't break jQuery chaining }; + //Cleans up particular elements, use like $('.blah').precendentCleanUp() + $.fn.precedentCleanUp = function(options){ + var matching_indexes = []; + var $els = this.filter('input, textarea'); + + //Clean up all the placeholders who have an el in the set of els + $.each(placeholders, function(i, placeholder){ + $.each($els, function(j, el){ + if(placeholder.$input[0] === el){ + placeholder.cleanUp(); + matching_indexes.push(i); + } + }); + }); + + //Remove all the ones we cleaned up from the list of placeholders + $.each(matching_ids, function(i, index){ + placeholders = placeholders.splice(index, 1); + }); + + return this; + }; + + //Cleans up all precedents, use like $.precedentCleanUp() + $.precendentCleanUp = function(options){ + $.each(placeholders, function(i, placeholder){ + placeholder.cleanUp(); + }); + + placeholders = []; + }; + })(jQuery); \ No newline at end of file From 60897b390c56c3fd776057888f4b5d3fc1a33ef2 Mon Sep 17 00:00:00 2001 From: Ben Taylor Date: Thu, 22 Mar 2012 12:16:51 +1100 Subject: [PATCH 2/2] Update js/jquery.precedent.js to fix bug where elements would push on to missing array --- js/jquery.precedent.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/jquery.precedent.js b/js/jquery.precedent.js index 5c5c905..765eb10 100644 --- a/js/jquery.precedent.js +++ b/js/jquery.precedent.js @@ -235,7 +235,7 @@ $.fn.precedent = function(options){ // Iterate over all the elements and initialise placeholder for them. this.filter('input, textarea').each(function() { - precendented_elements.push(new Placeholder($(this), options)); + placeholders.push(new Placeholder($(this), options)); }); return this; //Don't break jQuery chaining