summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRob Loach <robloach@gmail.com>2014-10-16 06:14:42 -0700
committerRob Loach <robloach@gmail.com>2014-10-16 06:14:42 -0700
commit09b340240d294f923ae2d0e90ac235830a0e71b0 (patch)
tree099d42f01010509d2c4b9965fdf6855307ea9995
parent57bf227077907c9358cc4e97f679585140bc91c6 (diff)
parent3fa3482b3c02221ae6380980630b35729fc763f9 (diff)
downloadjquery-once-09b340240d294f923ae2d0e90ac235830a0e71b0.zip
jquery-once-09b340240d294f923ae2d0e90ac235830a0e71b0.tar.gz
jquery-once-09b340240d294f923ae2d0e90ac235830a0e71b0.tar.bz2
Merge pull request #16 from theodoreb/once-id-required
Remove ability to call once() without a parameter
-rw-r--r--example/index.html4
-rw-r--r--jquery.once.js14
-rw-r--r--jquery.once.min.js6
-rw-r--r--test/test.js17
4 files changed, 19 insertions, 22 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..44ab452 100644
--- a/test/test.js
+++ b/test/test.js
@@ -1,12 +1,11 @@
-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("ID required", function() {
+ expect(1);
+ try {
+ $("#test1 span").once();
+ }
+ catch (e) {
+ ok(e, "Error is triggered when ID is missing.");
+ }
});
test(".once('test1-2') properly executed", function() {