Skip to content

Instantly share code, notes, and snippets.

@tmcgee123
Created October 21, 2015 21:14
Show Gist options
  • Save tmcgee123/c25325d70277b2ee3f3a to your computer and use it in GitHub Desktop.
Save tmcgee123/c25325d70277b2ee3f3a to your computer and use it in GitHub Desktop.
This is a polyfill/fallback for angular-locker when the browser does not support webstorage.
(function (angular) {
'use strict';
/**
* @ngdoc overview
* @name angularLockerPolyfill
* @description Holds the decorator that overrides angular-locker
*/
angular.module('angularLockerPolyfill', [
'angular-locker'
])
.config([
'$provide',
function ($provide) {
$provide.decorator('locker', [
'$delegate',
'$cacheFactory',
function ($delegate, $cacheFactory) {
var storage = {
local: {},
session: {}
},
LockerFallback = function (storageType) {
var self = this;
/**
* @ngdoc function
* @name get
* @description Gets the value from the cacheFactory using the passed in key or returns with the
* defaultValue
* @param {String} key - the key to retrieve the value for
* @param {Object} [defaultValue] - the default value to use if there is not one in the cache
* @returns {Object} Either the associated value in the cache or the passsed in defaultValue
*/
this.get = function (key, defaultValue) {
return storage[storageType].get(key) || defaultValue;
};
/**
* @ngdoc function
* @name put
* @description Stores the value in the cache using the key as a label
* @param {String} key - the key to retrieve the value for
* @param {Object|Function} value - the value to set in the "session" cache or a function that modifies
* the current value in the cache and returns it to be stored in the cache
*/
this.put = function (key, value) {
storage[storageType].put(key, _.isFunction(value) ? value(self.get(key)) : value);
};
/**
* @ngdoc function
* @name has
* @description Returns whether or not the key exists in storage
* @param {String} name - the key to see if there is an existing value
* @returns {Boolean} Whether or not there is a value for the associated key
*/
this.has = function (name) {
return !!storage[storageType].get(name);
};
/**
* @ngdoc function
* @name clean
* @description Clears all keys and values from the cache
*/
this.clean = function () {
storage[storageType].removeAll();
};
/**
* @ngdoc function
* @name sessionForget
* @description Clears all keys and values from the cache associated with the key
* @params {String} key - Clears the values associated with the key
*/
this.forget = function (key) {
storage[storageType].remove(key);
};
};
//determine if webstorage is supported, if not then use the cacheFactory
if (!$delegate.supported()) {
storage.local = $cacheFactory('local');
storage.session = $cacheFactory('session');
$delegate.driver = function (name) {
return new LockerFallback(name);
};
}
return $delegate;
}
]);
}
]);
}(window.angular));
@seven-cd
Copy link

Does this work with angular-locker 2.0.4?

@tmcgee123
Copy link
Author

@seven-cd I'm not sure, if they've changed any of the above methods being overwritten then it will likely need to be adapted to accommodate those changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment