jquery-once
Advanced tools
Comparing version 2.0.0-beta.2 to 2.0.0-beta.3
/*! | ||
* jQuery Once v2.0.0-beta.2 - http://github.com/robloach/jquery-once | ||
* jQuery Once v2.0.0-beta.3 - http://github.com/robloach/jquery-once | ||
* @license MIT, GPL-2.0 | ||
@@ -11,5 +11,5 @@ * http://opensource.org/licenses/MIT | ||
* | ||
* jQuery is a dependency, so we wrap the code with a UMD pattern in order to | ||
* allow loading jQuery and jQuery Once through a module definition like | ||
* CommonJS, AMD, or otherwise. | ||
* jQuery Once has a dependency on jQuery, so we wrap the code with a UMD | ||
* pattern in order to allow loading jQuery and jQuery Once through a module | ||
* definition like CommonJS, AMD, or through a global object. | ||
* | ||
@@ -34,15 +34,51 @@ * @see {@link http://github.com/umdjs/umd} | ||
/** | ||
* Filter elements by whether they have not yet been processed. | ||
* Ensures that the given ID is valid, returning "once" if one is not given. | ||
* | ||
* @param {string} id | ||
* The data id used to determine whether the given elements have already | ||
* been processed or not. | ||
* @param {string} [id="once"] | ||
* A string representing the ID to check. Defaults to `"once"`. | ||
* | ||
* @returns jQuery element collection of elements that have now run once by | ||
* the given id. | ||
* @returns The valid ID name. | ||
* | ||
* @throws Error when an ID is provided, but not a string. | ||
* @private | ||
*/ | ||
var checkId = function(id) { | ||
id = id || "once"; | ||
if (typeof id !== "string") { | ||
throw new Error("The jQuery Once id parameter must be a string"); | ||
} | ||
return id; | ||
}; | ||
/** | ||
* Filter elements that have yet to be processed by the given data ID. | ||
* | ||
* @param {string} [id="once"] | ||
* The data ID used to determine whether the given elements have already | ||
* been processed or not. Defaults to `"once"`. | ||
* | ||
* @returns jQuery collection of elements that have now run once by | ||
* the given ID. | ||
* | ||
* @example | ||
* // Change the color to green only once. | ||
* $('p').once('changecolor').css('color', 'green'); | ||
* ``` javascript | ||
* // The following will change the color of each paragraph to red, just once | ||
* // for the "changecolor" key. | ||
* $('p').once('changecolor').css('color', 'red'); | ||
* | ||
* // .once() will return a set of elements that yet to have the once ID | ||
* // associated with them. You can return to the original collection set by | ||
* // using .end(). | ||
* $('p') | ||
* .once("changecolorblue") | ||
* .css("color", "blue") | ||
* .end() | ||
* .css("color", "red"); | ||
* | ||
* // To execute a function on the once set, you can use jQuery's each(). | ||
* $('div.calendar').once().each(function() { | ||
* // Since there is no once ID provided here, the key will be "once". | ||
* }); | ||
* ``` | ||
* | ||
* @see removeOnce | ||
@@ -56,11 +92,6 @@ * @see findOnce | ||
$.fn.once = function (id) { | ||
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. | ||
var name = "jquery-once-" + id; | ||
// Build the jQuery Once data name from the provided ID. | ||
var name = "jquery-once-" + checkId(id); | ||
// Filter the elements by which do not have the data yet. | ||
// Find elements that don't have the jQuery Once data applied to them yet. | ||
return this.filter(function() { | ||
@@ -72,18 +103,24 @@ return $(this).data(name) !== true; | ||
/** | ||
* Removes the once data from the given elements, based on the given ID. | ||
* Removes the once data from elements, based on the given ID. | ||
* | ||
* @param {string} 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. | ||
* @param {string} [id="once"] | ||
* A 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. Defaults to `"once"`. | ||
* | ||
* @returns jQuery element collection of elements that now have their once | ||
* data removed. | ||
* @returns jQuery collection of elements that were acted upon to remove their | ||
* once data. | ||
* | ||
* @example | ||
* // Remove once data with the "changecolor" ID. | ||
* $('p').removeOnce('changecolor').each(function() { | ||
* // This function is called for all elements that had their once removed. | ||
* ``` javascript | ||
* // Remove once data with the "changecolor" ID. The result set is the | ||
* // elements that had their once data removed. | ||
* $('p').removeOnce("changecolor").css("color", ""); | ||
* | ||
* // Any jQuery function can be performed on the result set. | ||
* $("div.calendar").removeOnce().each(function() { | ||
* // Remove the calendar behavior. | ||
* }); | ||
* ``` | ||
* | ||
@@ -98,3 +135,3 @@ * @see once | ||
// Filter through the elements to find the once'd elements. | ||
return this.findOnce(id).removeData("jquery-once-" + id); | ||
return this.findOnce(id).removeData("jquery-once-" + checkId(id)); | ||
}; | ||
@@ -105,12 +142,13 @@ | ||
* | ||
* @param {string} 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 | ||
* @param {string} [id="once"] | ||
* A 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. | ||
* was originally passed to the once() function. Defaults to "once". | ||
* | ||
* @returns jQuery element collection of elements that have been run once. | ||
* @returns jQuery collection of elements that have been run once. | ||
* | ||
* @example | ||
* // Find all elements that have the changecolor'ed once. | ||
* ``` javascript | ||
* // Find all elements that have been changecolor'ed once. | ||
* $('p').findOnce('changecolor').each(function() { | ||
@@ -120,2 +158,9 @@ * // This function is called for all elements that has already once'd. | ||
* | ||
* // Find all elements that have been acted on with the default "once" key. | ||
* $('p').findOnce().each(function() { | ||
* // This function is called for all elements that have been acted on with | ||
* // a "once" action. | ||
* }); | ||
* ``` | ||
* | ||
* @see once | ||
@@ -129,3 +174,3 @@ * @this jQuery | ||
// Filter the elements by which do have the data. | ||
var name = "jquery-once-" + id; | ||
var name = "jquery-once-" + checkId(id); | ||
@@ -132,0 +177,0 @@ return this.filter(function() { |
/*! | ||
* jQuery Once v2.0.0-beta.2 - http://github.com/robloach/jquery-once | ||
* jQuery Once v2.0.0-beta.3 - http://github.com/robloach/jquery-once | ||
* @license MIT, GPL-2.0 | ||
@@ -7,3 +7,3 @@ * http://opensource.org/licenses/MIT | ||
*/ | ||
(function(e){"use strict";if(typeof exports==="object"){e(require("jquery"))}else if(typeof define==="function"&&define.amd){define(["jquery"],e)}else{e(jQuery)}})(function(e){"use strict";e.fn.once=function(n){n=n||"once";if(typeof n!=="string"){throw new Error("jQuery.once() parameter must be a string")}var t="jquery-once-"+n;return this.filter(function(){return e(this).data(t)!==true}).data(t,true)};e.fn.removeOnce=function(e){return this.findOnce(e).removeData("jquery-once-"+e)};e.fn.findOnce=function(n){var t="jquery-once-"+n;return this.filter(function(){return e(this).data(t)===true})}}); | ||
(function(e){"use strict";if(typeof exports==="object"){e(require("jquery"))}else if(typeof define==="function"&&define.amd){define(["jquery"],e)}else{e(jQuery)}})(function(e){"use strict";var n=function(e){e=e||"once";if(typeof e!=="string"){throw new Error("The jQuery Once id parameter must be a string")}return e};e.fn.once=function(t){var r="jquery-once-"+n(t);return this.filter(function(){return e(this).data(r)!==true}).data(r,true)};e.fn.removeOnce=function(e){return this.findOnce(e).removeData("jquery-once-"+n(e))};e.fn.findOnce=function(t){var r="jquery-once-"+n(t);return this.filter(function(){return e(this).data(r)===true})}}); | ||
//# sourceMappingURL=jquery.once.min.js.map |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "2.0.0-beta.2", | ||
"version": "2.0.0-beta.3", | ||
"author": { | ||
@@ -12,0 +12,0 @@ "name": "Rob Loach", |
@@ -5,3 +5,3 @@ { | ||
"description": "Act on jQuery elements only once.", | ||
"version": "2.0.0-beta.2", | ||
"version": "2.0.0-beta.3", | ||
"keywords": [ | ||
@@ -55,2 +55,3 @@ "jquery", | ||
"eslint": "~0.11.0-alpha.0", | ||
"jsdoc-to-markdown": "^0.5.9", | ||
"jsdom": "^2.0.0", | ||
@@ -66,4 +67,5 @@ "mocha": "*", | ||
"projectz": "./node_modules/.bin/projectz compile", | ||
"release": "./node_modules/.bin/uglifyjs jquery.once.js -o jquery.once.min.js --comments --source-map jquery.once.min.js.map --mangle" | ||
"docs": "./node_modules/.bin/jsdoc2md jquery.once.js > API.md", | ||
"build": "./node_modules/.bin/uglifyjs jquery.once.js -o jquery.once.min.js --comments --source-map jquery.once.min.js.map --mangle" | ||
} | ||
} |
@@ -42,3 +42,3 @@ | ||
- Install: `npm install --save jquery-once` | ||
- CDN URL: `//wzrd.in/bundle/jquery-once@2.0.0-beta.2` | ||
- CDN URL: `//wzrd.in/bundle/jquery-once@2.0.0-beta.3` | ||
@@ -62,79 +62,5 @@ ### [Ender](http://ender.jit.su/) | ||
### `.once(id)` | ||
See the [API documentation](API.md) for details on how to use jQuery Once. | ||
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').click(function() { | ||
// .once('calendar') filters out all elements which already have been | ||
// 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(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" | ||
// with the "calendar" ID. | ||
}); | ||
``` | ||
### `.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. | ||
#### Example | ||
``` javascript | ||
$('div.calendar').removeOnce('calendar').each(function() { | ||
// This function is called for each element whose once() data is removed. | ||
}); | ||
``` | ||
## Development | ||
@@ -146,16 +72,25 @@ | ||
Use [Mocha](http://mochajs.org) to test with [ESLint](http://eslint.org) and | ||
[Mocha JSDom](https://github.com/rstacruz/mocha-jsdom): | ||
Test with [ESLint](http://eslint.org), [Mocha](http://mochajs.org) and [Mocha | ||
JSDom](https://github.com/rstacruz/mocha-jsdom): | ||
npm test | ||
Update project documentation with [Projectz](https://github.com/bevry/projectz): | ||
Update project documentation with [Projectz](https://github.com/bevry/projectz) | ||
and [jsdoc-to-markdown](https://github.com/75lb/jsdoc-to-markdown): | ||
npm run-script projectz | ||
npm run projectz | ||
npm run docs | ||
Build the project with: | ||
Build `jquery.once.min.js` with: | ||
npm run-script release | ||
npm run build | ||
Tag and publish the new versions to [npm](http://npmjs.com) with [Semantic | ||
Versioning](http://semver.org/): | ||
git tag 2.0.0-alpha.3 | ||
git push origin 2.0.0-alpha.3 | ||
npm publish | ||
<!-- HISTORY/ --> | ||
@@ -162,0 +97,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19219
11
231
7
144