Skip to content

Commit

Permalink
Merge pull request #27 from mattdbridges/named_forms
Browse files Browse the repository at this point in the history
optionally name a form that is present on pages with different URLs
  • Loading branch information
simsalabim committed May 14, 2012
2 parents bce583a + 19a1531 commit 1dc5791
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
3 changes: 2 additions & 1 deletion sisyphus.js
Expand Up @@ -109,6 +109,7 @@
customKeyPrefix: "",
timeout: 0,
autoRelease: true,
name: null,
onSave: function() {},
onRestore: function() {},
onRelease: function() {}
Expand Down Expand Up @@ -142,7 +143,7 @@
targets = targets || {};
var self = this;
this.targets = this.targets || [];
this.href = location.hostname + location.pathname + location.search;
this.href = this.options.name || location.hostname + location.pathname + location.search;

this.targets = $.merge( this.targets, targets );
this.targets = $.unique( this.targets );
Expand Down
19 changes: 18 additions & 1 deletion spec/javascripts/fixtures/fixtures.html
Expand Up @@ -143,8 +143,25 @@
</div>
</form>
</fieldset>
<fieldset class="form-wrap sexy-border">
<legend class="form-legend">Form 3</legend>
<form name="named" id="named" method="get">
<div class="field">
<label for="yetAnotherName" class="input-label">Input Text <span class="code">[name="name"]</span>:</label>
<input type="text" name="name" value="" id="yetAnotherName" class="width-all dark"/>
</div>
<div class="field">
<label for="yetAnotherTextarea" class="input-label">Textarea <span class="code">[name="description"]</span>:</label>
<textarea name="description" cols="50" rows="10" id="yetAnotherTextarea" class="width-all dark"></textarea>
</div>
<div class="field">
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</div>
</form>
</fieldset>
<div class="submit-both">
<input type="button" value="Submit Both" id="sendAll" />
<input type="button" value="Submit All" id="sendAll" />
</div>
</div>

Expand Down
22 changes: 18 additions & 4 deletions spec/spec.js
@@ -1,11 +1,12 @@
describe("Sisyphus", function() {
var sisyphus, targetForm;
var sisyphus, targetForm, namedForm;

beforeEach( function() {
loadFixtures( "fixtures.html" );
sisyphus = Sisyphus.getInstance();
sisyphus.setOptions( {} );
targetForm = $( "#form1" );
namedForm = $( "#named" );
sisyphus.protect( targetForm );
} );

Expand All @@ -18,7 +19,7 @@ describe("Sisyphus", function() {
var sisyphus1 = Sisyphus.getInstance();
expect( typeof sisyphus1 ).toEqual( "object" );
} );

it( "should return null on freeing", function() {
sisyphus = Sisyphus.free();
expect( sisyphus ).toEqual( null );
Expand Down Expand Up @@ -77,6 +78,13 @@ describe("Sisyphus", function() {
expect( targets1 ).toBeLessThan( targets2 );
} );

it( "should allow a custom name for the form", function() {
sisyphus = Sisyphus.getInstance();
sisyphus.setOptions( { name: "something" } );
sisyphus.protect( namedForm );
expect ( sisyphus.href ).toEqual( "something" );
} );

it( "should not protect the same form twice and more times", function() {
// #form1 is already being protected from 'beforeEach' method
var targets1 = sisyphus.targets.length,
Expand Down Expand Up @@ -184,8 +192,14 @@ describe("jQuery.sisyphus", function() {
sisyphus = Sisyphus.getInstance();
expect( o ).toEqual( sisyphus );
} );



it( "should set the custom name on the Sisyphus instance", function() {
Sisyphus.free()
var o = $(" #named" ).sisyphus( { name: "custom-name" } );
sisyphus = Sisyphus.getInstance();
expect( o.href ).toEqual( "custom-name" );
} );

it( "should protect matched forms with Sisyphus", function() {
spyOn( Sisyphus.getInstance(), "protect" );
$( "#form1" ).sisyphus(),
Expand Down

0 comments on commit 1dc5791

Please sign in to comment.