gsklee / ngStorage

localStorage and sessionStorage done right for AngularJS.
MIT License
2.33k stars 461 forks source link

fire events when item is added or removed. #214

Open shivasingh0101 opened 8 years ago

shivasingh0101 commented 8 years ago

This is helpfull when working with tokens in my case, when token is deleted from localstorage (logout functionality) i want to show a dialog box in the same browser in all tabs saying that logged out on another tab, please reload. For this situation how i want to detect the toke is deleted. so may it is easy to have an events to fire or callback functionality.

Webrow commented 8 years ago

Well I am not the professional on the topic. But just use a watch.

pseudocode

$rootScope.$watch(function() {
        return $localStorage.sendQueue.length
    }, function watchCallback(newValue, oldValue) {
        if (newValue > oldValue) {
            $timeout(flushQueue, 3000);
        }
    });
knvpk commented 8 years ago

Hi @Webrow , Actually i want to call some function only when the token is deleted, but as you said it will fired as many times the token is updated in the localStorage which is not required. and Also rather tahn watch its easy to subscribe to events with respect to performance.

egilkh commented 8 years ago

Could probably add code to broadcast when changes occur. Then you would need to $rootScope.on() for that event. If you want to create a PR that would be cool. If not I'll add it to my longish list of things to do.