Skip to content

Instantly share code, notes, and snippets.

@mr-feek
Created July 29, 2016 21:52
Show Gist options
  • Save mr-feek/9be688441b7ae73d60c19fd1eda66c75 to your computer and use it in GitHub Desktop.
Save mr-feek/9be688441b7ae73d60c19fd1eda66c75 to your computer and use it in GitHub Desktop.
/**
* Created by Feek on 7/29/16.
*/
define([
'backbone',
'marionette'
], function (
Backbone,
Marionette
) {
var StorageHelper = Marionette.Object.extend({
storageAvailable: null,
initialize: function(options) {
this.storageAvailable = this.checkIfStorageIsAvailable();
},
/**
* determine whether or not we have access to storage on this device
* @returns {boolean}
*/
checkIfStorageIsAvailable: function () {
try {
var storage = window['sessionStorage'],
x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch (e) {
return false;
}
},
getItem: function (key) {
if (this.storageAvailable) {
return window.sessionStorage.getItem(key);
}
},
setItem: function(key, value) {
if (this.storageAvailable) {
return window.sessionStorage.setItem(key, value);
}
}
});
return StorageHelper;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment