tymondesigns / angular-locker

🗄️ A simple & configurable abstraction for local/session storage in angular js projects
https://npm.im/angular-locker
MIT License
314 stars 42 forks source link

new function changeNamespace #23

Closed mvaullerin closed 9 years ago

mvaullerin commented 9 years ago

Allow user to change the namespace globally.

tymondesigns commented 9 years ago

What's the use-case for this? As apposed to either setting the default namespace at the config stage or using the namespace() setter

mvaullerin commented 9 years ago

First, sorry if it's not really understandable, as French my English is obviously pretty bad :)

I develop a plateform for events, and I need the namespace to be the ID of the event for all the angular app (because someone can be connected to the plateform for two events in the same time). And I set the namespace after checking the ID correspond to a valid event.

If I don't have this method, I have to use the namespace() setter dans specify the namespace all the time. (or I missed something)

tymondesigns commented 9 years ago

Any reason you can't set it in the config stage? e.g.

.config(['lockerProvider', function config(lockerProvider) {
    lockerProvider.defaults({
        namespace: 'myApp',
    });
}]);
mvaullerin commented 9 years ago

The ID is dynamic, I get it in a controller.

tymondesigns commented 9 years ago

Ok, I see what you mean. I would implement something a little different than this PR to achieve this though.

I'm thinking maybe a second parameter to the namespace and driver methods, to set the value on the same instance.

This is just a convenience though, since you could, as you say, always use the namespace setter each time you call a method.

Having said that, I would suggest that you wrap this in your own service if you haven't already. e.g.

.factory('Store', function (locker) {

    var namespace = 'default';

    return {
        setNamespace: function (ns) {
            namespace = ns;
            return this;
        },
        get: function (key, def) {
            return locker.namespace(namespace).get(key, def);
        },
        put: function (key, val, def) {
            return locker.namespace(namespace).put(key, val, def);
        },
        // etc
    };
});

// then in your controller
Store.setNamespace('foo');
Store.get('bar');

Or not :) but you get the idea

mvaullerin commented 9 years ago

Good idea, I will try this. Thanks :) And thanks for this module, it's really awesome :)