Permalink
Browse files

#2 add 'excludeFields' option

  • Loading branch information...
1 parent b001b39 commit f415cc3c5ef1dadee2740af8c6b8d25db6ba262e @simsalabim simsalabim committed Nov 21, 2011
Showing with 35 additions and 8 deletions.
  1. +19 −3 sisyphus.js
  2. +1 −1 sisyphus.min.js
  3. +15 −4 spec/spec.js
View
@@ -79,7 +79,6 @@
this.targets = $.merge( this.targets, targets );
this.targets = $.unique( this.targets );
this.targets = $( this.targets );
-
if ( ! this.isLocalStorageAvailable() ) {
return false;
}
@@ -122,8 +121,12 @@
self.targets.each( function() {
var targetFormId = $( this ).attr( "id" );
var fieldsToProtect = $( this ).find( ":input" ).not( ":submit" ).not( ":reset" ).not( ":button" );
-
+
fieldsToProtect.each( function() {
+ if ( $.inArray( this, self.options.excludeFields ) !== -1 ) {
+ // Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
+ return true;
+ }
var field = $( this );
var prefix = self.href + targetFormId + field.attr( "name" ) + self.options.customKeyPrefix;
if ( field.is( ":text" ) || field.is( "textarea" ) ) {
@@ -150,7 +153,12 @@
self.targets.each( function() {
var targetFormId = $( this ).attr( "id" );
var fieldsToProtect = $( this ).find( ":input" ).not( ":submit" ).not( ":reset" ).not( ":button" );
+
fieldsToProtect.each( function() {
+ if ( $.inArray( this, self.options.excludeFields ) !== -1 ) {
+ // Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
+ return true;
+ }
var field = $( this );
var prefix = self.href + targetFormId + field.attr( "name" ) + self.options.customKeyPrefix;
var value = field.val();
@@ -194,8 +202,12 @@
var target = $( this );
var targetFormId = target.attr( "id" );
var fieldsToProtect = target.find( ":input" ).not( ":submit" ).not( ":reset" ).not( ":button" );
-
+
fieldsToProtect.each( function() {
+ if ( $.inArray( this, self.options.excludeFields ) !== -1 ) {
+ // Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
+ return true;
+ }
var field = $( this );
var prefix = self.href + targetFormId + field.attr( "name" ) + self.options.customKeyPrefix;
var resque = localStorage.getItem( prefix );
@@ -347,6 +359,10 @@
var released = false;
var self = this;
fieldsToProtect.each( function() {
+ if ( $.inArray( this, self.options.excludeFields ) !== -1 ) {
+ // Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
+ return true;
+ }
var field = $( this );
var prefix = self.href + targetFormId + field.attr( "name" ) + self.options.customKeyPrefix;
localStorage.removeItem( prefix );
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -140,13 +140,13 @@ describe("Sisyphus", function() {
it( "should fire callback on saving data to Local Storage", function() {
spyOn( sisyphus.options, "onSave" );
sisyphus.saveToLocalStorage( "key", "value" );
- expect(sisyphus.options.onSave).toHaveBeenCalled();
+ expect( sisyphus.options.onSave ).toHaveBeenCalled();
} );
it( "should fire callback on removing data from Local Storage", function() {
spyOn( sisyphus.options, "onRelease" );
sisyphus.releaseData( targetForm.attr( "id" ), targetForm.find( ":text" ) );
- expect(sisyphus.options.onRelease).toHaveBeenCalled();
+ expect( sisyphus.options.onRelease ).toHaveBeenCalled();
} );
it( "should fire callback on restoring data from Local Storage", function() {
@@ -155,7 +155,18 @@ describe("Sisyphus", function() {
} );
spyOn( sisyphus.options, "onRestore" );
sisyphus.restoreAllData();
- expect(sisyphus.options.onRestore).toHaveBeenCalled();
+ expect( sisyphus.options.onRestore ).toHaveBeenCalled();
+ } );
+
+ it( "should not store excluded fields data", function() {
+ sisyphus.setOptions( {
+ excludeFields: $( "textarea:first" )
+ })
+ $( "textarea:first" ).val( "should not store" );
+ sisyphus.saveAllData();
+ $( "textarea:first" ).val( "" );
+ sisyphus.restoreAllData();
+ expect( $( "textarea:first" ).val() ).toEqual( "" );
} );
});
@@ -178,7 +189,7 @@ describe("jQuery.sisyphus", function() {
it( "should protect matched forms with Sisyphus", function() {
spyOn( Sisyphus.getInstance(), "protect" );
$( "#form1" ).sisyphus(),
- expect(Sisyphus.getInstance().protect).toHaveBeenCalled();
+ expect( Sisyphus.getInstance().protect ).toHaveBeenCalled();
} );
});

0 comments on commit f415cc3

Please sign in to comment.