Skip to content

Instantly share code, notes, and snippets.

@surferxo3
Last active May 7, 2020 11:26
Show Gist options
  • Save surferxo3/82e65af7a911ca793d5b34ef24b2a273 to your computer and use it in GitHub Desktop.
Save surferxo3/82e65af7a911ca793d5b34ef24b2a273 to your computer and use it in GitHub Desktop.
AMD Web Storage Utils for Underscore.js
// @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