npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngstorage

v0.3.11

Published

ngStorage =========

Downloads

82,946

Readme

ngStorage

Build Status Dependency Status devDependency Status

An AngularJS module that makes Web Storage working in the Angular Way. Contains two services: $localStorage and $sessionStorage.

Differences with Other Implementations

  • No Getter 'n' Setter Bullshit - Right from AngularJS homepage: "Unlike other frameworks, there is no need to [...] wrap the model in accessors methods. Just plain old JavaScript here." Now you can enjoy the same benefit while achieving data persistence with Web Storage.

  • sessionStorage - We got this often-overlooked buddy covered.

  • Cleanly-Authored Code - Written in the Angular Way, well-structured with testability in mind.

  • No Cookie Fallback - With Web Storage being readily available in all the browsers AngularJS officially supports, such fallback is largely redundant.

Install

Bower

bower install ngstorage

NOTE: We are ngstorage and NOT ngStorage. The casing is important!

NPM

npm install ngstorage

NOTE: We are ngstorage and NOT ngStorage. The casing is important!

nuget

Install-Package gsklee.ngStorage

Or search for Angular ngStorage in the nuget package manager. https://www.nuget.org/packages/gsklee.ngStorage

CDN

cdnjs

cdnjs now hosts ngStorage at https://cdnjs.com/libraries/ngStorage

To use it

<script src="https://cdnjs.cloudflare.com/ajax/libs/ngStorage/0.3.6/ngStorage.min.js"></script>

jsDelivr

jsDelivr hosts ngStorage at http://www.jsdelivr.com/#!ngstorage

To use is

<script src="https://cdn.jsdelivr.net/ngstorage/0.3.6/ngStorage.min.js"></script>

Usage

Require ngStorage and Inject the Services

angular.module('app', [
    'ngStorage'
]).controller('Ctrl', function(
    $scope,
    $localStorage,
    $sessionStorage
){});

Read and Write | Demo

Pass $localStorage (or $sessionStorage) by reference to a hook under $scope in plain ol' JavaScript:

$scope.$storage = $localStorage;

And use it like you-already-know:

<body ng-controller="Ctrl">
    <button ng-click="$storage.counter = $storage.counter + 1">{{$storage.counter}}</button>
</body>

Optionally, specify default values using the $default() method:

$scope.$storage = $localStorage.$default({
    counter: 42
});

With this setup, changes will be automatically sync'd between $scope.$storage, $localStorage, and localStorage - even across different browser tabs!

Read and Write Alternative (Not Recommended) | Demo

If you're not fond of the presence of $scope.$storage, you can always use watchers:

$scope.counter = $localStorage.counter || 42;

$scope.$watch('counter', function() {
    $localStorage.counter = $scope.counter;
});

$scope.$watch(function() {
    return angular.toJson($localStorage);
}, function() {
    $scope.counter = $localStorage.counter;
});

This, however, is not the way ngStorage is designed to be used with. As can be easily seen by comparing the demos, this approach is way more verbose, and may have potential performance implications as the values being watched quickly grow.

Delete | Demo

Plain ol' JavaScript again, what else could you better expect?

// Both will do
delete $scope.$storage.counter;
delete $localStorage.counter;

This will delete the corresponding entry inside the Web Storage.

Delete Everything | Demo

If you wish to clear the Storage in one go, use the $reset() method:

$localStorage.$reset();

Optionally, pass in an object you'd like the Storage to reset to:

$localStorage.$reset({
    counter: 42
});

Permitted Values | Demo

You can store anything except those not supported by JSON:

  • Infinity, NaN - Will be replaced with null.
  • undefined, Function - Will be removed.

Usage from config phase

To read and set values during the Angular config phase use the .get/.set functions provided by the provider.

var app = angular.module('app', ['ngStorage'])
.config(['$localStorageProvider',
    function ($localStorageProvider) {
        $localStorageProvider.get('MyKey');

        $localStorageProvider.set('MyKey', { k: 'value' });
    }]);

Prefix

To change the prefix used by ngStorage use the provider function setKeyPrefix during the config phase.

var app = angular.module('app', ['ngStorage'])
.config(['$localStorageProvider',
    function ($localStorageProvider) {
        $localStorageProvider.setKeyPrefix('NewPrefix');
    }])

Custom serialization

To change how ngStorage serializes and deserializes values (uses JSON by default) you can use your own functions.

angular.module('app', ['ngStorage'])
.config(['$localStorageProvider', 
  function ($localStorageProvider) {
    var mySerializer = function (value) {
      // Do what you want with the value.
      return value;
    };
    
    var myDeserializer = function (value) {
      return value;
    };

    $localStorageProvider.setSerializer(mySerializer);
    $localStorageProvider.setDeserializer(myDeserializer);
  }];)

Minification

Just run $ npm install to install dependencies. Then run $ grunt for minification.

Hints

Watch the watch

ngStorage internally uses an Angular watch to monitor changes to the $storage/$localStorage objects. That means that a digest cycle is required to persist your new values into the browser local storage. Normally this is not a problem, but, for example, if you launch a new window after saving a value...

$scope.$storage.school = theSchool;
$log.debug("launching " + url);
var myWindow = $window.open("", "_self");
myWindow.document.write(response.data);

the new values will not reliably be saved into the browser local storage. Allow a digest cycle to occur by using a zero-value $timeout as:

$scope.$storage.school = theSchool;
$log.debug("launching and saving the new value" + url);
$timeout(function(){
   var myWindow = $window.open("", "_self");
   myWindow.document.write(response.data);
});

or better using $scope.$evalAsync as:

$scope.$storage.school = theSchool;
$log.debug("launching and saving the new value" + url);
$scope.$evalAsync(function(){
   var myWindow = $window.open("", "_self");
   myWindow.document.write(response.data);
});

And your new values will be persisted correctly.

Todos

  • ngdoc Documentation
  • Namespace Support
  • Unit Tests
  • Grunt Tasks

Any contribution will be appreciated.