Skip to content

Commit

Permalink
Fix to sessionStorage Model to surpress QUOTA DOMExceptions when Safa…
Browse files Browse the repository at this point in the history
…ri users are in private browsing mode
  • Loading branch information
carlfeberhard committed Mar 2, 2015
1 parent 6224e22 commit 0c94f04
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
18 changes: 14 additions & 4 deletions client/galaxy/scripts/mvc/base-mvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var SessionStorageModel = Backbone.Model.extend({
if( !options.silent ){
model.trigger( 'request', model, {}, options );
}
var returned;
var returned = {};
switch( method ){
case 'create' : returned = this._create( model ); break;
case 'read' : returned = this._read( model ); break;
Expand All @@ -111,9 +111,19 @@ var SessionStorageModel = Backbone.Model.extend({

/** set storage to the stringified item */
_create : function( model ){
var json = model.toJSON(),
set = sessionStorage.setItem( model.id, JSON.stringify( json ) );
return ( set === null )?( set ):( json );
try {
var json = model.toJSON(),
set = sessionStorage.setItem( model.id, JSON.stringify( json ) );
return ( set === null )?( set ):( json );
// DOMException is thrown in Safari if in private browsing mode and sessionStorage is attempted:
// http://stackoverflow.com/questions/14555347
// TODO: this could probably use a more general soln - like detecting priv. mode + safari => non-ajaxing Model
} catch( err ){
if( !( ( err instanceof DOMException ) && ( navigator.userAgent.indexOf("Safari") > -1 ) ) ){
throw err;
}
}
return null;
},

/** read and parse json from storage */
Expand Down
18 changes: 14 additions & 4 deletions static/scripts/mvc/base-mvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var SessionStorageModel = Backbone.Model.extend({
if( !options.silent ){
model.trigger( 'request', model, {}, options );
}
var returned;
var returned = {};
switch( method ){
case 'create' : returned = this._create( model ); break;
case 'read' : returned = this._read( model ); break;
Expand All @@ -111,9 +111,19 @@ var SessionStorageModel = Backbone.Model.extend({

/** set storage to the stringified item */
_create : function( model ){
var json = model.toJSON(),
set = sessionStorage.setItem( model.id, JSON.stringify( json ) );
return ( set === null )?( set ):( json );
try {
var json = model.toJSON(),
set = sessionStorage.setItem( model.id, JSON.stringify( json ) );
return ( set === null )?( set ):( json );
// DOMException is thrown in Safari if in private browsing mode and sessionStorage is attempted:
// http://stackoverflow.com/questions/14555347
// TODO: this could probably use a more general soln - like detecting priv. mode + safari => non-ajaxing Model
} catch( err ){
if( !( ( err instanceof DOMException ) && ( navigator.userAgent.indexOf("Safari") > -1 ) ) ){
throw err;
}
}
return null;
},

/** read and parse json from storage */
Expand Down
2 changes: 1 addition & 1 deletion static/scripts/packed/mvc/base-mvc.js

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

0 comments on commit 0c94f04

Please sign in to comment.