diff options
-rw-r--r-- | example/index.html | 4 | ||||
-rw-r--r-- | jquery.once.js | 14 | ||||
-rw-r--r-- | jquery.once.min.js | 6 | ||||
-rw-r--r-- | test/test.js | 11 |
4 files changed, 11 insertions, 24 deletions
diff --git a/example/index.html b/example/index.html index 2279967..a002fb4 100644 --- a/example/index.html +++ b/example/index.html @@ -15,10 +15,10 @@ $('p').once('changecolor').css('color', 'red'); // Now change the background color. - $('p').once().css('background-color', 'black'); + $('p').once('changebackground').css('background-color', 'black'); // Change it again. - $('p').once().css('background-color', 'yellow'); + $('p').once('changebackground').css('background-color', 'yellow'); </script> </body> </html> diff --git a/jquery.once.js b/jquery.once.js index 0b2bbb0..5469ab7 100644 --- a/jquery.once.js +++ b/jquery.once.js @@ -30,18 +30,13 @@ } }(function ($) { "use strict"; - var uuid = 0; /** * Filter elements by whether they have not yet been processed. * * @param {string} [id] - * (Optional) The data id used to determine whether the given elements have - * already been processed or not. - * - * When id is not provided, it becomes a unique identifier, depicted as a - * number. The element's data id will then be represented in the form of - * "jquery-once-#". + * The data id used to determine whether the given elements have already + * been processed or not. * * @returns jQuery element collection of elements that have now run once by * the given id. @@ -58,9 +53,12 @@ * @public */ $.fn.once = function (id) { + if (!id) { + throw new Error("An ID is required when calling jQuery.once()"); + } // 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); + var name = "jquery-once-" + id; // Filter the elements by which do not have the data yet. return this.filter(function() { diff --git a/jquery.once.min.js b/jquery.once.min.js index 5c222a8..1e29ed1 100644 --- a/jquery.once.min.js +++ b/jquery.once.min.js @@ -1,4 +1,4 @@ -/*! jQuery Once - v2.0.0-alpha.6 - 10/7/2014 - https://github.com/RobLoach/jquery-once - * (c) 2014 Rob Loach (http://github.com/robloach) +/*! jQuery Once - v2.0.0-alpha.8 - 10/15/2014 - https://github.com/RobLoach/jquery-once + * (c) 2014 Rob Loach (http://github.com/RobLoach) * Licensed GPL-2.0, MIT */ -!function(a){"use strict";"object"==typeof exports?a(require("jquery")):"function"==typeof define&&define.amd?define(["jquery"],a):a(jQuery)}(function(a){"use strict";var b={},c=0;a.fn.once=function(d){"string"!=typeof d&&(d in b||(b[d]=++c),d=b[d]);var e="jquery-once-"+d;return this.filter(function(){return a(this).data(e)!==!0}).data(e,!0)},a.fn.removeOnce=function(a){return this.findOnce(a).removeData("jquery-once-"+a)},a.fn.findOnce=function(b){var c="jquery-once-"+b;return this.filter(function(){return a(this).data(c)===!0})}});
\ No newline at end of file +!function(a){"use strict";"object"==typeof exports?a(require("jquery")):"function"==typeof define&&define.amd?define(["jquery"],a):a(jQuery)}(function(a){"use strict";a.fn.once=function(b){if(!b)throw new Error("An ID is required when calling jQuery.once()");var c="jquery-once-"+b;return this.filter(function(){return a(this).data(c)!==!0}).data(c,!0)},a.fn.removeOnce=function(a){return this.findOnce(a).removeData("jquery-once-"+a)},a.fn.findOnce=function(b){var c="jquery-once-"+b;return this.filter(function(){return a(this).data(c)===!0})}});
\ No newline at end of file diff --git a/test/test.js b/test/test.js index d1dbd2f..e50ac76 100644 --- a/test/test.js +++ b/test/test.js @@ -1,14 +1,3 @@ -test(".once() properly executed", function() { - // Create one once() call. - $('#test1 span').once().data('test1', 'foo'); - - // Create another once() call. - $('#test1 span').once().data('test1', 'bar'); - - var data = $('#test1 span').data('test1'); - 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'); |