diff options
author | Rob Loach <robloach@gmail.com> | 2014-09-26 15:18:02 -0700 |
---|---|---|
committer | Rob Loach <robloach@gmail.com> | 2014-09-26 15:18:02 -0700 |
commit | 8f09cc05ab25ac12b66088e4c84bb368884f0d52 (patch) | |
tree | 63c1ad55bd866de96337c43941c4e8a67fca22ec | |
parent | ac1fd448edf1efa9c0abbc14d9c8642952498a19 (diff) | |
parent | c696fcfaffb0bbc02ad1407ff5baa6086f8ba510 (diff) | |
download | jquery-once-8f09cc05ab25ac12b66088e4c84bb368884f0d52.zip jquery-once-8f09cc05ab25ac12b66088e4c84bb368884f0d52.tar.gz jquery-once-8f09cc05ab25ac12b66088e4c84bb368884f0d52.tar.bz2 |
Merge pull request #10 from RobLoach/next
Update to 2.0.0-alpha.1
-rw-r--r-- | README.md | 29 | ||||
-rw-r--r-- | bower.json | 4 | ||||
-rw-r--r-- | component.json | 2 | ||||
-rw-r--r-- | composer.json | 2 | ||||
-rw-r--r-- | jquery.once.js | 34 | ||||
-rw-r--r-- | once.jquery.json | 4 | ||||
-rw-r--r-- | package.json | 10 | ||||
-rw-r--r-- | test/test.js | 26 |
8 files changed, 54 insertions, 57 deletions
@@ -1,12 +1,10 @@ -jQuery Once -=========== +# jQuery Once [](https://travis-ci.org/RobLoach/jquery-once) Filters out all elements that had the same filter applied on them before. It can be used to ensure that a function is only applied once to an element. -Usage ------ +## Usage ``` javascript $('div.calendar').once('calendar', function() { @@ -14,10 +12,10 @@ $('div.calendar').once('calendar', function() { // code segment is executed repeatedly. }); $('div.calendar').once('calendar').click(function() { - // .once('calendar') filters out all elements which already have the - // class 'calendar'. It applies that class to the remaining elements - // and leaves them in the jQuery object. - // The previous set of elements can be restored with .end() + // .once('calendar') filters out all elements which already have been + // filtered with once(), and the elements that haven't been filtered + // yet remain. The previous set of elements can be restored with + // .end(). }); ``` @@ -27,14 +25,13 @@ It also works without supplying a name: $('div.calendar').once(function() { // This function is only executed once for each div, even if this // code segment is executed repeatedly. Other scripts can't refer - // to this `once` method and the class names used are in the form - // of jquery-once-1 and so on. + // to this `once` method. The once data used to store execution are + // in the form "jquery-once-1", "jquery-once-2", etc. }); ``` -Development ------------ +## Development Leverage [npm](http://npmjs.org), [grunt](http://gruntjs.com), and [qunit](http://qunitjs.com): @@ -46,14 +43,12 @@ $ grunt release ``` -License -------- +## License -Dual licensed under the MIT and GPL licenses. +Dual licensed under the [MIT and GPL licenses](LICENSE). -Credits -------- +## Credits * [Konstantin Käfer](http://kkaefer.com) * [Rob Loach](http://robloach.net) @@ -2,7 +2,7 @@ "name": "jquery-once", "homepage": "http://github.com/robloach/jquery-once", "description": "jQuery Once Plugin", - "version": "1.2.6", + "version": "2.0.0-alpha.1", "main": "jquery.once.js", "ignore": [ "*.json", @@ -16,6 +16,6 @@ "jquery" ], "dependencies": { - "jquery": ">=1.8.0" + "jquery": ">=2.1.1" } } diff --git a/component.json b/component.json index 18fd98e..7f8345a 100644 --- a/component.json +++ b/component.json @@ -4,7 +4,7 @@ "homepage": "http://github.com/robloach/jquery-once", "description": "jQuery Once Plugin", "license": "MIT", - "version": "1.2.6", + "version": "2.0.0-alpha.1", "keywords": [ "jquery" ], diff --git a/composer.json b/composer.json index ab1bc4f..92a0a43 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "GPL-2.0" ], "require": { - "components/jquery": ">=1.5" + "components/jquery": ">=2.1.1" }, "extra": { "component": { diff --git a/jquery.once.js b/jquery.once.js index 4117dbf..5faa4c0 100644 --- a/jquery.once.js +++ b/jquery.once.js @@ -1,5 +1,5 @@ /** - * jQuery Once Plugin 1.2.6 + * jQuery Once Plugin 2.0.0-alpha.1 * http://github.com/robloach/jquery-once * * Dual licensed under the MIT and GPL licenses: @@ -24,20 +24,16 @@ * Filters elements by whether they have not yet been processed. * * @param id - * (Optional) If this is a string, then it will be used as the CSS class - * name that is applied to the elements for determining whether it has - * already been processed. The elements will get a class in the form of - * "id-processed". + * (Optional) If this is a string, then it will be the data ID used + * to determine whether it has already been processed or not. * * If the id parameter is a function, it will be passed off to the fn * parameter and the id will become a unique identifier, represented as a * number. * * When the id is neither a string or a function, it becomes a unique - * identifier, depicted as a number. The element's class will then be - * represented in the form of "jquery-once-#-processed". - * - * Take note that the id must be valid for usage as an element's class name. + * identifier, depicted as a number. The element's data ID will then be + * represented in the form of "jquery-once-#". * @param fn * (Optional) If given, this function will be called for each element that * has not yet been processed. The function's return value follows the same @@ -57,11 +53,14 @@ if (!fn) { fn = id; } - id = 'jquery-once-' + cache[id]; + id = cache[id]; } - // Remove elements from the set that have already been processed. - var name = id + '-processed'; - var elements = this.not('.' + name).addClass(name); + + // Filter the elements by which do not have the data yet. + var name = 'jquery-once-' + id; + var elements = this.filter(function() { + return $(this).data(name) !== true; + }).data(name, true); return $.isFunction(fn) ? elements.each(fn) : elements; }; @@ -70,7 +69,7 @@ * Filters elements that have been processed once already. * * @param id - * A required string representing the name of the class which should be used + * 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. @@ -84,8 +83,11 @@ * @api public */ $.fn.removeOnce = function (id, fn) { - var name = id + '-processed'; - var elements = this.filter('.' + name).removeClass(name); + // Filter the elements by which do have the data. + var name = 'jquery-once-' + id; + var elements = this.filter(function() { + return $(this).data(name) === true; + }).removeData(name); return $.isFunction(fn) ? elements.each(fn) : elements; }; diff --git a/once.jquery.json b/once.jquery.json index 6c202c9..58f0e4e 100644 --- a/once.jquery.json +++ b/once.jquery.json @@ -6,7 +6,7 @@ "jquery", "once" ], - "version": "1.2.6", + "version": "2.0.0-alpha.1", "author": { "name": "Konstantin Kafer", "url": "http://kkaefer.com" @@ -31,6 +31,6 @@ "homepage": "https://github.com/RobLoach/jquery-once", "download": "https://github.com/RobLoach/jquery-once/archive/master.zip", "dependencies": { - "jquery": ">=1.5" + "jquery": "~2.1.1" } } diff --git a/package.json b/package.json index 86fc074..28a2610 100644 --- a/package.json +++ b/package.json @@ -22,13 +22,13 @@ } ], "dependencies": { - "jquery": "~1.8.0" + "jquery": "~2.1.1" }, "devDependencies": { - "grunt": "~0.4.1", - "grunt-contrib-qunit": "~0.2.1", - "grunt-contrib-jshint": "~0.4.3", - "grunt-contrib-uglify": "~0.1.1" + "grunt": "~0.4.5", + "grunt-contrib-qunit": "~0.5.2", + "grunt-contrib-jshint": "~0.10.0", + "grunt-contrib-uglify": "~0.6.0" }, "scripts": { "test": "grunt jshint qunit" diff --git a/test/test.js b/test/test.js index c93cdfd..a0c3d58 100644 --- a/test/test.js +++ b/test/test.js @@ -26,36 +26,36 @@ test("Called only once", function() { // Verify that it was only called once. var count = $('#test2 span').data('count'); - ok(count === 1); + ok(count === 1, 'It was called ' + count + ' times.'); }); -test("Apply the class correctly", function() { +test("Apply the value to data correctly", function() { // Verify that the element starts without the class. - var hasClass = $('#test3 span').hasClass('test3-processed'); - ok(!hasClass, 'Processed class not applied in the beginning.'); + var hasData = $('#test3 span').data('jquery-once-test3'); + ok(!hasData, 'Value not applied in the beginning.'); // Create one once() call. $('#test3 span').once('test3', function() { // Do nothing. }); - // Verify the class is applied. - hasClass = $('#test3 span').hasClass('test3-processed'); - ok(hasClass, 'The processed class is properly applied after once().'); + // Verify the data is applied. + hasData = $('#test3 span').data('jquery-once-test3'); + ok(hasData, 'The value is properly applied after once().'); }); -test("Remove the class correctly", function() { +test("Remove the value from attribute correctly", function() { // Create one once() call. $('#test4 span').once('test4', function() { // Do nothing. }); - // Verify the class is applied. - var hasClass = $('#test4 span').hasClass('test4-processed'); - ok(hasClass, 'The processed class is properly applied after once().'); + // Verify the data is applied. + var hasData = $('#test4 span').data('jquery-once-test4'); + ok(hasData, 'The value is properly applied after once().'); // Remove the once property. $('#test4 span').removeOnce('test4'); - hasClass = $('#test4 span').hasClass('test4-processed'); - ok(!hasClass, 'The processed class is properly removed when called removeOnce().'); + hasData = $('#test4 span').data('jquery-once-test4'); + ok(!hasData, 'The value is properly removed when called removeOnce().'); }); |