Skip to content

Instantly share code, notes, and snippets.

@simonwoo
Created November 28, 2016 09:46
Show Gist options
  • Select an option

  • Save simonwoo/ae709ceaa7a16474ed62d8fad30a6c88 to your computer and use it in GitHub Desktop.

Select an option

Save simonwoo/ae709ceaa7a16474ed62d8fad30a6c88 to your computer and use it in GitHub Desktop.

Revisions

  1. simonwoo revised this gist Nov 28, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion angular_localstorage.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    angular.module('restlet.hub.utils')
    angular.module('localstorage')
    .factory('storageHelper', function ($cacheFactory, $window) {

    'use strict';
  2. simonwoo created this gist Nov 28, 2016.
    41 changes: 41 additions & 0 deletions angular_localstorage.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    angular.module('restlet.hub.utils')
    .factory('storageHelper', function ($cacheFactory, $window) {

    'use strict';

    var memoryStorage = $cacheFactory('XXX');
    var localStorage = $window.localStorage;

    return {
    storage: getStorage()
    };

    function getStorage () {

    if (isLocalStorageSupported()) {
    // return localstorage directly,
    // because chrome does not support method forwarding on localstorage
    return localStorage;

    } else {
    // use memory storage,
    // because safari in private mode does not allow the use of localstorage
    return {
    getItem: memoryStorage.get,
    setItem: memoryStorage.put,
    removeItem: memoryStorage.remove
    };
    }
    }

    function isLocalStorageSupported () {
    try {
    localStorage.setItem('test', 'testValue');
    localStorage.removeItem('test');
    return true;
    } catch (error) {
    return false;
    }
    }

    });