summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRob Loach <robloach@gmail.com>2014-12-19 00:44:51 -0500
committerRob Loach <robloach@gmail.com>2014-12-19 00:44:51 -0500
commit041b74c37e5480c9273d0228e635ca34a9b7fa6d (patch)
treeb6915100762faf811e3c248ba43fe1d75b6b43c1
parentbaea88a270eb2260a7b95d11e62164d9068f705f (diff)
downloadjquery-once-041b74c37e5480c9273d0228e635ca34a9b7fa6d.zip
jquery-once-041b74c37e5480c9273d0228e635ca34a9b7fa6d.tar.gz
jquery-once-041b74c37e5480c9273d0228e635ca34a9b7fa6d.tar.bz2
Add documentation
-rw-r--r--README.md55
-rw-r--r--jquery.once.js2
2 files changed, 46 insertions, 11 deletions
diff --git a/README.md b/README.md
index 80ae96b..d5f59b9 100644
--- a/README.md
+++ b/README.md
@@ -58,29 +58,51 @@ be used to ensure that a function is only applied once to an element.
## Usage
-### `.once()`
+### `.once(id)`
Filter elements by whether they have not yet been processed.
+#### Parameters
+
+* `id` *string* (optional) The data id used to determine whether the given elements have
+already been processed or not. Default: `once`
+
+#### Returns
+
+jQuery element collection of elements that have now run once by the given id.
+
+#### Example
+
``` javascript
-$('div.calendar').once('calendar').each(function() {
- // This function is only executed once for each div, even if this
- // code segment is executed repeatedly.
-});
$('div.calendar').once('calendar').click(function() {
// .once('calendar') filters out all elements which already have been
- // filtered with once(), and the elements that haven't been filtered
+ // filtered with once('calendar'), and the elements that haven't been filtered
// yet remain. The previous set of elements can be restored with
// .end().
});
+$('div.calendar').once().each(function() {
+ // This function is only executed once for each div, even if this
+ // code segment is executed repeatedly. Keyed by "once".
+});
```
-### `.findOnce()`
+### `.findOnce(id)`
After `.once()` is used and you need to retrieve all elements that have already
been executed with `.once()`, you can use the `.findOnce() function:
+#### Parameters
+
+* `id` *string* The data id used to determine whether the given elements have
+already been processed or not.
+
+#### Returns
+
+jQuery element collection of elements that have been run once.
+
+#### Example
+
``` javascript
$('div.calendar').findOnce('calendar').each(function() {
// This function is called for each element that was already called "once"
@@ -88,10 +110,23 @@ $('div.calendar').findOnce('calendar').each(function() {
});
```
-### `.removeOnce()`
+### `.removeOnce(id)`
+
+A required string representing the name of the data id which should be used when
+filtering the elements. This only filters elements that have already been processed by
+the once function. The id should be the same id that was originally passed to the
+`once()` function.
+
+#### Parameters
+
+* `id` *string* The data id used to determine whether the given elements have
+already been processed or not.
+
+#### Returns
+
+jQuery element collection of elements that now have their once data removed.
-It is possible to remove the `.once()` data, and iterate through each element
-whose once state is removed:
+#### Example
``` javascript
$('div.calendar').removeOnce('calendar').each(function() {
diff --git a/jquery.once.js b/jquery.once.js
index c8e606c..a13b9f8 100644
--- a/jquery.once.js
+++ b/jquery.once.js
@@ -53,7 +53,7 @@
$.fn.once = function (id) {
id = id || "once";
if (typeof id !== "string") {
- throw new Error("jQuery.once() parameter MUST be a 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.