Last active
May 7, 2020 11:26
-
-
Save surferxo3/82e65af7a911ca793d5b34ef24b2a273 to your computer and use it in GitHub Desktop.
AMD Web Storage Utils for Underscore.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @module Utils.Extended | |
define( | |
'Utils.Extended' | |
, [ | |
'Utils' | |
, 'Backbone' | |
, 'SC.Configuration' | |
, 'underscore' | |
] | |
, function ( | |
Utils | |
, Backbone | |
, Configuration | |
, _ | |
) | |
{ | |
'use strict'; | |
// @class Utils.Extended @extends Utils | |
return _.extend(Utils, { | |
storageGet : function(key) { | |
if (typeof(Storage) !== 'undefined') { | |
var value = localStorage.getItem(key); | |
try { | |
value = value && JSON.parse(value); | |
} catch(e) {} | |
return value; | |
} else { | |
console.log('Sorry! No Web Storage support...'); | |
} | |
} | |
, storageSet : function(key, value) { | |
if (typeof(Storage) !== 'undefined') { | |
if (_.isObject(value)) { | |
value = JSON.stringify(value); | |
} | |
localStorage.setItem(key, value); | |
} else { | |
console.log('Sorry! No Web Storage support...'); | |
} | |
} | |
, storageRemove : function(key) { | |
if (typeof(Storage) !== 'undefined') { | |
localStorage.removeItem(key); | |
} else { | |
console.log('Sorry! No Web Storage support...'); | |
} | |
} | |
, storageClear : function() { | |
if (typeof(Storage) !== 'undefined') { | |
_.keys(localStorage).forEach(function(key) { | |
if (key.startsWith('vehicle_')) { | |
this.storageRemove(key); | |
} | |
}.bind(this)); | |
} else { | |
console.log('Sorry! No Web Storage support...'); | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment