GithubHelp home page GithubHelp logo

baotuo / ngstorage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gsklee/ngstorage

0.0 1.0 0.0 149 KB

localStorage and sessionStorage done right for AngularJS.

License: MIT License

JavaScript 100.00%

ngstorage's Introduction

###** Looking for Maintainers! **

ngStorage

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 install ngstorage

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.

Minification

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

Todos

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

Any contribution will be appreciated.

ngstorage's People

Contributors

alexgorbatchev avatar baotuo avatar chbrown avatar dcendents avatar dotansimha avatar dpogue avatar egilkh avatar jcompagner avatar jlkalberer avatar kbaltrinic avatar passy avatar patrickjs avatar roaks3 avatar stylenclass avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.