Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
simsalabim committed Nov 11, 2011
1 parent 01c326d commit 2633df1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
59 changes: 36 additions & 23 deletions sisyphus.js
Expand Up @@ -29,30 +29,42 @@ var Sisyphus = ( function() {


/**
* Set plugin options
* Set plugin initial options
*
* @param [Object] options
*
* @return void
*/
setOptions: function ( options ) {
setInitialOptions: function ( options ) {
var defaults = {
excludeFields: null,
customKeyPrefix: "",
timeout: 0,
onSaveCallback: function() {},
onRestoreCallback: function() {},
onReleaseDataCallback: function() {}
onSave: function() {},
onRestore: function() {},
onRelease: function() {}
};
this.options = this.options || $.extend( defaults, options );
},

/**
* Set plugin options
*
* @param [Object] options
*
* @return void
*/
setOptions: function ( options ) {
this.options = this.options || this.setInitialOptions( options );
this.options = $.extend( this.options, options );
},


/**
* Protect specified forms, store it's fields data to local storage and restore them on page load
*
* @param [Object] targets forms object(s), result of jQuery selector
* @param Object options plugin options
* @param [Object] targets forms object(s), result of jQuery selector
* @param Object options plugin options
*
* @return void
*/
Expand Down Expand Up @@ -157,8 +169,8 @@ var Sisyphus = ( function() {
self.saveToLocalStorage( prefix, value, false );
} );
} );
if ( $.isFunction( self.options.onSaveCallback ) ) {
self.options.onSaveCallback.call();
if ( $.isFunction( self.options.onSave ) ) {
self.options.onSave.call();
}
},

Expand Down Expand Up @@ -188,17 +200,17 @@ var Sisyphus = ( function() {
} );
} );

if ( restored && $.isFunction( self.options.onRestoreCallback ) ) {
self.options.onRestoreCallback.call();
if ( restored && $.isFunction( self.options.onRestore ) ) {
self.options.onRestore.call();
}
},


/**
* Restore form field data from local storage
*
* @param Object field jQuery form fieldent object
* @param String resque previously stored fields data
* @param Object field jQuery form element object
* @param String resque previously stored fields data
*
* @return void
*/
Expand All @@ -221,8 +233,8 @@ var Sisyphus = ( function() {
/**
* Bind immediate saving (on typing/checking/changing) field data to local storage when user fills it
*
* @param Object field jQuery form fieldent object
* @param String prefix prefix used as key to store data in local storage
* @param Object field jQuery form element object
* @param String prefix prefix used as key to store data in local storage
*
* @return void
*/
Expand Down Expand Up @@ -257,17 +269,17 @@ var Sisyphus = ( function() {
} catch (e) {
//QUOTA_EXCEEDED_ERR
}
if ( fireCallback && value !== "" && $.isFunction( this.options.onSaveCallback ) ) {
this.options.onSaveCallback.call();
if ( fireCallback && value !== "" && $.isFunction( this.options.onSave ) ) {
this.options.onSave.call();
}
},


/**
* Bind saving field data on change
*
* @param Object field jQuery form fieldent object
* @param String prefix prefix used as key to store data in local storage
* @param Object field jQuery form element object
* @param String prefix prefix used as key to store data in local storage
*
* @return void
*/
Expand Down Expand Up @@ -305,7 +317,7 @@ var Sisyphus = ( function() {
bindReleaseData: function() {
var self = this;
self.targets.each( function( i ) {
var target = $( this);
var target = $( this );
var fieldsToProtect = target.find( ":input" ).not( ":submit" ).not( ":reset" ).not( ":button" );
var formId = target.attr( "id" );
$( this ).bind( "submit reset", function() {
Expand All @@ -321,7 +333,7 @@ var Sisyphus = ( function() {
* Bind release form fields data from local storage on submit/resett form
*
* @param String targetFormId
* @param Object fieldsToProtect jQuery object contains form fields to protect
* @param Object fieldsToProtect jQuery object contains form fields to protect
*
* @return void
*/
Expand All @@ -335,8 +347,8 @@ var Sisyphus = ( function() {
released = true;
} );

if ( released && $.isFunction( self.options.onReleaseDataCallback ) ) {
self.options.onReleaseDataCallback.call();
if ( released && $.isFunction( self.options.onRelease ) ) {
self.options.onRelease.call();
}
}

Expand All @@ -348,6 +360,7 @@ var Sisyphus = ( function() {
getInstance: function() {
if ( ! params.instantiated ) {
params.instantiated = init();
params.instantiated.setInitialOptions();
}
return params.instantiated;
},
Expand Down
2 changes: 1 addition & 1 deletion sisyphus.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions spec/spec.js
Expand Up @@ -131,22 +131,22 @@ describe("Sisyphus", function() {
} );


it( "should fire callback ONCE on saving all data to Local Storage", function() {
spyOn( sisyphus.options, "onSaveCallback" );
it( "should fire callback once on saving all data to Local Storage", function() {
spyOn( sisyphus.options, "onSave" );
sisyphus.saveAllData();
expect( sisyphus.options.onSaveCallback.callCount ).toEqual( 1 );
expect( sisyphus.options.onSave.callCount ).toEqual( 1 );
} );

it( "should fire callback on saving data to Local Storage", function() {
spyOn( sisyphus.options, "onSaveCallback" );
spyOn( sisyphus.options, "onSave" );
sisyphus.saveToLocalStorage( "key", "value" );
expect(sisyphus.options.onSaveCallback).toHaveBeenCalled();
expect(sisyphus.options.onSave).toHaveBeenCalled();
} );

it( "should fire callback on removing data from Local Storage", function() {
spyOn( sisyphus.options, "onReleaseDataCallback" );
spyOn( sisyphus.options, "onRelease" );
sisyphus.releaseData( targetForm.attr( "id" ), targetForm.find( ":text" ) );
expect(sisyphus.options.onReleaseDataCallback).toHaveBeenCalled();
expect(sisyphus.options.onRelease).toHaveBeenCalled();
} );

});
Expand Down

0 comments on commit 2633df1

Please sign in to comment.