Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically change the storage type from controller itself. #315

Open
numberknot opened this issue May 13, 2016 · 5 comments
Open

Dynamically change the storage type from controller itself. #315

numberknot opened this issue May 13, 2016 · 5 comments

Comments

@numberknot
Copy link

numberknot commented May 13, 2016

Looking to use both Local Storage as well as Session Storage. Iniitally Configured the module to use 'localstorage' localStorageServiceProvider.setStorageType('localStorage').

But What if I want to session storage somewhere in middle of the application.

There should be some setStorageType function which we can call from controller that dynamically change storage type.

@douwekiela
Copy link

I agree that this would be nice. A workaround might be to set the storage type to localStorage and then do something like this to clear storage when the session ends:

$window.onbeforeunload = function (e) { localStorageService.clearAll(); }

@yeluolei
Copy link

yeluolei commented Jul 3, 2016

ask for this feature too. I want to store something in the local storage to persistent, but I also want something in session storage that will be deleted when the user close the window. I supports to add some api like
set(key, value ,storageType)

@zpgu
Copy link
Contributor

zpgu commented Jul 12, 2016

Desperately need this too, so cooked up this simple patch.

Right before a set, switch type with either of these:
localStorageService.setStorageType('localStorage');
localStorageService.setStorageType('sessionStorage');

or use:
set(key, value, storageType)

if storageType omitted, then previous type is used. Use setStorageType to reset, so the default for your project is not disturbed.

--- angular-local-storage.js
+++ angular-local-storage.js
@@ -100,7 +100,7 @@
return prefix + key;
};
// Checks the browser to see if local storage is supported

  •  var browserSupportsLocalStorage = (function () {
    
  •  var checkSupport = function () {
     try {
       var supported = (storageType in $window && $window[storageType] !== null);
    

@@ -122,12 +122,18 @@
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return false;
}

  •  }());
    
  •  };
    
  •  var browserSupportsLocalStorage = checkSupport();
    

    // Directly adds a value to local storage
    // If local storage is not available in the browser use cookies
    // Example use: localStorageService.add('library','angular');

  •  var addToLocalStorage = function (key, value) {
    
  •  var addToLocalStorage = function (key, value, type) {
    
  •    if (type) {
    
  •      setStorageType(type);
    
  •    }
    
  •  // Let's convert undefined values to null to get the value consistent
     if (isUndefined(value)) {
       value = null;
    

    @@ -388,6 +394,11 @@
    return storageType;
    };

  •    var setStorageType = function(type) {
    
  •      storageType = type;
    
  •      browserSupportsLocalStorage = checkSupport();
    
  •    };
    
  •  // Add a listener on scope variable to save its changes to local storage
     // Return a function which when called cancels binding
     var bindToScope = function(scope, key, def, lsKey) {
    

    @@ -423,6 +434,7 @@
    return {
    isSupported: browserSupportsLocalStorage,
    getStorageType: getStorageType,

  •      setStorageType: setStorageType,
       set: addToLocalStorage,
       add: addToLocalStorage, //DEPRECATED
       get: getFromLocalStorage,
    

@zpgu
Copy link
Contributor

zpgu commented Jul 12, 2016

the patch doesn't display as it should. (+ - are messed up)

@grevory
Copy link
Owner

grevory commented Jul 13, 2016

#320 by @zpgu addresses this issue. I encourage you to review his changes before I accept the PR. Thanks for the feedback guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants