diff options
author | Rob Loach <robloach@gmail.com> | 2014-01-29 11:44:30 -0500 |
---|---|---|
committer | Rob Loach <robloach@gmail.com> | 2014-01-29 11:44:30 -0500 |
commit | 70664bbe119c8f3e1ff3ce56ae1412df8763a8ce (patch) | |
tree | 5b47035a28290246423ea59bdd41a24044c6b89b | |
parent | 2b719c62f2a7132252008691a58787c4e44951a5 (diff) | |
download | jquery-once-70664bbe119c8f3e1ff3ce56ae1412df8763a8ce.zip jquery-once-70664bbe119c8f3e1ff3ce56ae1412df8763a8ce.tar.gz jquery-once-70664bbe119c8f3e1ff3ce56ae1412df8763a8ce.tar.bz2 |
Fix to use data attributes rather than an array
-rw-r--r-- | example/index.html | 3 | ||||
-rw-r--r-- | jquery.once.js | 36 | ||||
-rw-r--r-- | test/test.js | 24 |
3 files changed, 25 insertions, 38 deletions
diff --git a/example/index.html b/example/index.html index fa009dc..448fbd3 100644 --- a/example/index.html +++ b/example/index.html @@ -2,7 +2,7 @@ <html> <head> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> - <script src="../jquery.once.min.js"></script> + <script src="../jquery.once.js"></script> </head> <body> <h1>jQuery Once</h1> @@ -11,6 +11,7 @@ $('p').once('greenify', function() { $(this).css('color', 'green'); }); + $('p').once('bluify').css('color', 'blue'); </script> </body> </html> diff --git a/jquery.once.js b/jquery.once.js index 71e9268..322d3f7 100644 --- a/jquery.once.js +++ b/jquery.once.js @@ -57,20 +57,14 @@ if (!fn) { fn = id; } - id = 'jquery-once-' + cache[id]; + id = cache[id]; } - /** - * Adds the ID at the end of attribute value. - */ - function addID (index, value) { - return $.trim((value || '') + ' ' + id); - } - - var elements = this - // Remove elements from the set that have already been processed. - .not('[data-jquery-once~="' + id + '"]') - .attr('data-jquery-once', addID); + // 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; }; @@ -93,19 +87,11 @@ * @api public */ $.fn.removeOnce = function (id, fn) { - /** - * Removes the ID from the attribute value. - */ - function removeID (index, value) { - return $.trim(value.replace(id, '')) - // Split and join to keep the value clean. - .split(/\s+/g) - .join(' '); - } - - var elements = this - .filter('[data-jquery-once~="' + id + '"]') - .attr('data-jquery-once', removeID); + // 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/test/test.js b/test/test.js index 457f1e7..a0c3d58 100644 --- a/test/test.js +++ b/test/test.js @@ -26,22 +26,22 @@ 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 value to attribute correctly", function() { +test("Apply the value to data correctly", function() { // Verify that the element starts without the class. - var hasClass = ($('#test3 span').attr('data-jquery-once') || '').indexOf('test3') !== -1; - ok(!hasClass, 'Value 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').attr('data-jquery-once').indexOf('test3') !== -1; - ok(hasClass, 'The value 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 value from attribute correctly", function() { @@ -50,12 +50,12 @@ test("Remove the value from attribute correctly", function() { // Do nothing. }); - // Verify the class is applied. - var hasClass = $('#test4 span').attr('data-jquery-once').indexOf('test4') !== -1; - ok(hasClass, 'The value 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').attr('data-jquery-once').indexOf('test4') !== -1; - ok(!hasClass, 'The value is properly removed when called removeOnce().'); + hasData = $('#test4 span').data('jquery-once-test4'); + ok(!hasData, 'The value is properly removed when called removeOnce().'); }); |