diff options
author | Rob Loach <robloach@gmail.com> | 2014-10-08 14:55:22 -0400 |
---|---|---|
committer | Rob Loach <robloach@gmail.com> | 2014-10-08 14:55:22 -0400 |
commit | a10ba8f605279314a3bac2f5c61c7b6fea98ab88 (patch) | |
tree | 37460e36854cb91d457853d4fea6c9e55713bc0a | |
parent | 4c862ae108fc69e17495f4169400ba9f41439228 (diff) | |
download | jquery-once-a10ba8f605279314a3bac2f5c61c7b6fea98ab88.zip jquery-once-a10ba8f605279314a3bac2f5c61c7b6fea98ab88.tar.gz jquery-once-a10ba8f605279314a3bac2f5c61c7b6fea98ab88.tar.bz2 |
Remove unneeded cache variable
-rw-r--r-- | HISTORY.md | 3 | ||||
-rw-r--r-- | example/index.html | 6 | ||||
-rw-r--r-- | jquery.once.js | 14 | ||||
-rw-r--r-- | once.jquery.json | 2 | ||||
-rw-r--r-- | package.json | 4 | ||||
-rw-r--r-- | test/test.js | 23 |
6 files changed, 35 insertions, 17 deletions
@@ -1,5 +1,8 @@ # History +## v2.0.0-alpha.7 October 8, 2014 +- Removed unneeded cache variable + ## v2.0.0-alpha.6 October 5, 2014 - Removed function callback in order to promote jQuery chaining standards diff --git a/example/index.html b/example/index.html index 4b73760..f9b1e6a 100644 --- a/example/index.html +++ b/example/index.html @@ -13,6 +13,12 @@ // Attempt to change the color again. $('p').once('changecolor').css('color', 'red'); + + // Now change the background color. + $('p').once().css('background-color', 'black'); + + // Change it again. + $('p').once().css('background-color', 'yellow'); </script> </body> </html> diff --git a/jquery.once.js b/jquery.once.js index 054ceb4..c6e48b3 100644 --- a/jquery.once.js +++ b/jquery.once.js @@ -18,7 +18,7 @@ } }(function ($) { "use strict"; - var cache = {}, uuid = 0; + var uuid = 0; /** * Filter elements by whether they have not yet been processed. @@ -44,17 +44,11 @@ * @global */ $.fn.once = function (id) { - if (typeof id !== 'string') { - // Generate a numeric ID if the id passed is not a string. - if (!(id in cache)) { - cache[id] = ++uuid; - } - id = cache[id]; - } + // Build the name for the data identifier. Generate a new unique id if the + // id parameter is not provided. + var name = 'jquery-once-' + (id || ++uuid); // Filter the elements by which do not have the data yet. - var name = 'jquery-once-' + id; - return this.filter(function() { return $(this).data(name) !== true; }).data(name, true); diff --git a/once.jquery.json b/once.jquery.json index d8323ae..62c6bd4 100644 --- a/once.jquery.json +++ b/once.jquery.json @@ -6,7 +6,7 @@ "jquery", "once" ], - "version": "2.0.0-alpha.6", + "version": "2.0.0-alpha.7", "author": { "name": "Rob Loach", "url": "http://robloach.net" diff --git a/package.json b/package.json index 29e73b1..04aa7d5 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "jquery-once", "title": "jQuery Once", "description": "Act on jQuery elements only once.", - "version": "2.0.0-alpha.6", + "version": "2.0.0-alpha.7", "keywords": [ "jquery" ], @@ -61,4 +61,4 @@ "release": "grunt release", "jsdoc": "npm install jsdoc && node_modules/.bin/jsdoc jquery.once.js" } -}
\ No newline at end of file +} diff --git a/test/test.js b/test/test.js index 88d59e7..d1dbd2f 100644 --- a/test/test.js +++ b/test/test.js @@ -1,12 +1,27 @@ -test("Properly executed", function() { +test(".once() properly executed", function() { // Create one once() call. - $('#test1 span').once().data('test1', 'foobar'); + $('#test1 span').once().data('test1', 'foo'); + + // Create another once() call. + $('#test1 span').once().data('test1', 'bar'); var data = $('#test1 span').data('test1'); - ok(data === "foobar"); + ok(data === "bar"); +}); + +test(".once('test1-2') properly executed", function() { + // Create one once('test1-2') call. + $('#test1 span').once('test1-2').data('test1-2', 'foo'); + + // Create another once('test1-2') call. + $('#test1 span').once('test1-2').data('test1-2', 'bar'); + + // The data should result to the first once() call. + var data = $('#test1 span').data('test1-2'); + ok(data === "foo"); }); -test("Called only once", function() { +test("Called only once, counted", function() { // Count the number of times once() was called. $('#test2 span').data('count', 0); |