A simple & configurable abstraction for local/session storage in angular projects - providing a fluent api that is powerful and easy to use.
$ bower install angular-locker
$ npm install angular-locker
http://www.jsdelivr.com/#!angular.locker
Simply download the zip file HERE and include dist/angular-locker.min.js
in your project.
1.66 KB Minified & gzipped.
Add angular-locker
as a dependency
angular.module('myApp', ['angular-locker'])
Configure via lockerProvider
(optional)
.config(['lockerProvider', function config(lockerProvider) {
lockerProvider.defaults({
driver: 'session',
namespace: 'myApp',
separator: '.',
eventsEnabled: true,
extend: {}
});
}]);
Note: You can also pass false
to namespace
if you prefer to not have a namespace in your keys.
inject locker
into your controller/service/directive etc
.factory('MyFactory', ['locker', function MyFactory(locker) {
locker.put('someKey', 'someVal');
}]);
You can pass in an implementation of the Storage Interface to the lockerProvider
as described above. e.g.
lockerProvider.defaults({
extend: {
myCustomStore: function () {
// getItem
// setItem
// removeItem
// etc
}
}
});
// then use as normal
locker.driver('myCustomStore').put('foo', 'bar');
See my storageMock for an example on how to define a custom implementation.
There may be times where you will want to dynamically switch between using local and session storage.To achieve this, simply chain the driver()
setter to specify what storage driver you want to use, as follows:
// put an item into session storage
locker.driver('session').put('sessionKey', ['some', 'session', 'data']);
// this time use local storage
locker.driver('local').put('localKey', ['some', 'persistent', 'things']);
// add an item within a different namespace
locker.namespace('otherNamespace').put('foo', 'bar');
Omitting the driver or namespace setters will respect whatever default was specified via lockerProvider
.
there are several ways to add something to locker:
You can add Objects, Arrays, whatever :)
locker will automatically serialize your objects/arrays in local/session storage
locker.put('someString', 'anyDataType');
locker.put('someObject', { foo: 'I will be serialized', bar: 'pretty cool eh' });
locker.put('someArray', ['foo', 'bar', 'baz']);
// etc
Inserts specified key and return value of function
locker.put('someKey', function() {
var obj = { foo: 'bar', bar: 'baz' };
// some other logic
return obj;
});
The current value will be passed into the function so you can perform logic on the current value, before returning it. e.g.
locker.put('someKey', ['foo', 'bar']);
locker.put('someKey', function(current) {
current.push('baz');
return current;
});
locker.get('someKey'); // = ['foo', 'bar', 'baz']
If the current value is not already set then you can pass a third parameter as a default that will be returned instead. e.g.
// given locker.get('foo') is not defined
locker.put('foo', function (current) {
// current will equal 'bar'
}, 'bar');
This will add each key/value pair as a separate item in storage
locker.put({
someKey: 'johndoe',
anotherKey: ['some', 'random', 'array'],
boolKey: true
});
Inserts each item from the returned Object, similar to above
locker.put(function() {
// some logic
return {
foo: ['lorem', 'ipsum', 'dolor'],
user: {
username: 'johndoe',
displayName: 'Johnny Doe',
active: true,
role: 'user'
}
};
});
For this functionality you can use the add()
method.
If the key already exists then no action will be taken and false
will be returned
locker.add('someKey', 'someVal'); // true or false - whether the item was added or not
// locker.put('fooArray', ['bar', 'baz', 'bob']);
locker.get('fooArray'); // ['bar', 'baz', 'bob']
if the key does not exist then, if specified the default will be returned
locker.get('keyDoesNotExist', 'a default value'); // 'a default value'
You may pass an array to the get()
method to return an Object containing the specified keys (if they exist)
locker.get(['someKey', 'anotherKey', 'foo']);
// will return something like...
{
someKey: 'someValue',
anotherKey: true,
foo: 'bar'
}
You can also retrieve an item and then delete it via the pull()
method
// locker.put('someKey', { foo: 'bar', baz: 'bob' });
locker.pull('someKey', 'defaultVal'); // { foo: 'bar', baz: 'bob' }
// then...
locker.get('someKey', 'defaultVal'); // 'defaultVal'
You can retrieve all items within the current namespace
This will return an object containing all the key/value pairs in storage
locker.all();
// or
locker.namespace('somethingElse').all();
To count the number of items within a given namespace:
locker.count();
// or
locker.namespace('somethingElse').count();
You can determine whether an item exists in the current namespace via
locker.has('someKey'); // true or false
// or
locker.namespace('foo').has('bar');
// e.g.
if (locker.has('user.authToken') ) {
// we're logged in
} else {
// go to login page or something
}
The simplest way to remove an item is to pass the key to the forget()
method
locker.forget('keyToRemove');
// or
locker.driver('session').forget('sessionKey');
// etc..
You can also pass an array.
locker.forget(['keyToRemove', 'anotherKeyToRemove', 'something', 'else']);
you can remove all the items within the currently set namespace via the clean()
method
locker.clean();
// or
locker.namespace('someOtherNamespace').clean();
locker.empty();
There are 3 events that can be fired during various operations, these are:
// fired when a new item is added to storage
$rootScope.$on('locker.item.added', function (e, payload) {
// payload is equal to:
{
driver: 'local', // the driver that was set when the event was fired
namespace: 'locker', // the namespace that was set when the event was fired
key: 'foo', // the key that was added
value: 'bar' // the value that was added
}
});
// fired when an item is removed from storage
$rootScope.$on('locker.item.forgotten', function (e, payload) {
// payload is equal to:
{
driver: 'local', // the driver that was set when the event was fired
namespace: 'locker', // the namespace that was set when the event was fired
key: 'foo', // the key that was removed
}
});
// fired when an item's value changes to something new
$rootScope.$on('locker.item.updated', function (e, payload) {
// payload is equal to:
{
driver: 'local', // the driver that was set when the event was fired
namespace: 'locker', // the namespace that was set when the event was fired
key: 'foo', // the key that was updated
oldValue: 'bar', // the value that was set before the item was updated
newValue: 'baz' // the new value that the item was updated to
}
});
You can bind a scope property to a key in storage. Whenever the $scope value changes, it will automatically be persisted in storage. e.g.
app.controller('AppCtrl', ['$scope', function ($scope) {
locker.bind($scope, 'foo');
$scope.foo = ['bar', 'baz'];
locker.get('foo'); // = ['bar', 'baz']
}]);
You can also set a default value via the third parameter:
app.controller('AppCtrl', ['$scope', function ($scope) {
locker.bind($scope, 'foo', 'someDefault');
$scope.foo; // = 'someDefault'
locker.get('foo'); // = 'someDefault'
}]);
To unbind the $scope property, simply use the unbind method:
app.controller('AppCtrl', ['$scope', function ($scope) {
locker.unbind($scope, 'foo');
$scope.foo; // = undefined
locker.get('foo'); // = undefined
}]);
IE8 is not supported because I am utilising Object.keys()
To check if the browser natively supports local and session storage, you can do the following:
if (! locker.supported()) {
// load a polyfill?
}
I would recommend using Remy's Storage polyfill if you want to support older browsers.
For the latest browser compatibility chart see HERE
Take care to maintain the existing coding style using the provided .jscsrc
file. Add unit tests for any new or changed functionality. Lint and test your code using Gulp.
$ npm install
$ gulp
The MIT License (MIT)
Copyright (c) 2014 Sean Tymon
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.
Angular 是一款十分流行且好用的 Web 前端框架,目前由 Google 维护。这个条目收录的是 Angular 2 及其后面的版本。由于官方已将 Angular 2 和之前的版本 Angular.js 分开维护(两者的 GitHub 地址和项目主页皆不相同),所以就有了这个页面。传送门:Angular.js 特性 跨平台 渐进式 Web 应用 借助现代化 Web 平台的力量,交付 app
即将到来的Angular 2框架是使用TypeScript开发的。 因此Angular和TypeScript一起使用非常简单方便。 Angular团队也在其文档里把TypeScript视为一等公民。 正因为这样,你总是可以在Angular 2官网(或Angular 2官网中文版)里查看到最新的结合使用Angular和TypeScript的参考文档。 在这里查看快速上手指南,现在就开始学习吧!
从头开始创建项目 lint你的代码 运行您的单元测试和端到端测试。 Angular 2 CLI目前只在TypeScript中生成框架,稍后还会有其他版本。
这小节内容是译者加的,因为我认为对于新手而言,学习一个框架是有成本的,特别是对于一个不算简单的技术来说,我希望这篇教程是对新手友好的,所以我首先要让你放心的将时间和精力投入到Angular2 中。那我们先不谈技术细节,先用数据说话。 这里我多说一句,最近看一些文章中谷歌趋势截图,大都没有把范围限定在“编程”上。图中可以看出Vue2非常少,所以在下面比较中不再单独统计。 教程数量 这里我选取的主要是
我们已经在Highcharts Configuration Syntax一章中看到了用于绘制图表的配置 。 下面给出角度计图表的示例。 配置 (Configurations) 现在让我们看一下所采取的其他配置/步骤。 chart.type 将图表类型配置为基于计量。 将类型设置为“规格”。 var chart = { type: 'guage' }; pane 此类型仅适用于极坐标图和角度
角度计图表用于绘制仪表/仪表类型图表。 在本节中,我们将讨论不同类型的角度计图表。 Sr.No. 图表类型和描述 1 角度计 角度表。 2 实心仪表 实心图表。 3 Clock 时钟。 4 带双轴的仪表 带双轴的仪表图。 5 VU表 VU表图表。