summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThéodore Biadala <theodore@biadala.net>2014-12-15 11:39:38 +0100
committerThéodore Biadala <theodore@biadala.net>2014-12-15 11:39:38 +0100
commit779c426bf61ec43488a91bac78faf3971333e4cf (patch)
tree54a03afe7b3e83ec3489dd719a9b710896656e4c
parent03027a958db2e5af6c49156f97b8879cdc612a65 (diff)
downloadjquery-once-779c426bf61ec43488a91bac78faf3971333e4cf.zip
jquery-once-779c426bf61ec43488a91bac78faf3971333e4cf.tar.gz
jquery-once-779c426bf61ec43488a91bac78faf3971333e4cf.tar.bz2
Add a default value for once name and check once parameter is a string
fix #20
-rw-r--r--jquery.once.js5
-rw-r--r--jquery.once.min.js4
-rw-r--r--test/test.js29
3 files changed, 30 insertions, 8 deletions
diff --git a/jquery.once.js b/jquery.once.js
index 4664218..87741f9 100644
--- a/jquery.once.js
+++ b/jquery.once.js
@@ -53,8 +53,9 @@
* @public
*/
$.fn.once = function (id) {
- if (!id) {
- throw new Error("An ID is required when calling jQuery.once()");
+ id = id || "once";
+ if (typeof id !== "string") {
+ throw new Error("jQuery.once() parameter MUST be a string");
}
// Build the name for the data identifier. Generate a new unique id if the
// id parameter is not provided.
diff --git a/jquery.once.min.js b/jquery.once.min.js
index 45302c6..ff683be 100644
--- a/jquery.once.min.js
+++ b/jquery.once.min.js
@@ -1,4 +1,4 @@
-/*! jQuery Once - v2.0.0-alpha.9 - 10/16/2014 - https://github.com/RobLoach/jquery-once
+/*! jQuery Once - v2.0.0-alpha.9 - 12/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";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
+!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=b||"once","string"!=typeof b)throw new Error("jQuery.once() parameter MUST be a string");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 44ab452..4f35b66 100644
--- a/test/test.js
+++ b/test/test.js
@@ -1,10 +1,10 @@
-test("ID required", function() {
+test("String ID required", function() {
expect(1);
try {
- $("#test1 span").once();
+ $("#test1 span").once(function () {});
}
catch (e) {
- ok(e, "Error is triggered when ID is missing.");
+ ok(e, "Error is triggered when ID is not a string.");
}
});
@@ -20,7 +20,7 @@ test(".once('test1-2') properly executed", function() {
ok(data === "foo");
});
-test("Called only once, counted", function() {
+test("Called only once with name, counted", function() {
// Count the number of times once() was called.
$('#test2 span').data('count', 0);
@@ -41,6 +41,27 @@ test("Called only once, counted", function() {
ok(count === 1, 'It was called ' + count + ' times.');
});
+test("Called only once without name, counted", function() {
+ // Count the number of times once() was called.
+ $('#test2 span').data('once', 0);
+
+ // Create the once() callback.
+ var callback = function() {
+ var count = $('#test2 span').data('once');
+ count++;
+ $('#test2 span').data('once', count);
+ };
+
+ // Call once() a bunch of times.
+ for (var i = 0; i < 10; i++) {
+ $('#test2 span').once().each(callback);
+ }
+
+ // Verify that it was only called once.
+ var count = $('#test2 span').data('once');
+ ok(count === 1, 'It was called ' + count + ' times.');
+});
+
test("Apply the value to data correctly", function() {
// Verify that the element starts without the class.
var hasData = $('#test3 span').data('jquery-once-test3');