angular-identity-map
An identity map pattern implementation for AngularJS applications.
Requirements
AngularJS v1.0+ traverse
Getting started
with bower
Just include "angular-identity-map" to your dependencies list and run bower install
with npm
npm install angular-identity-map
manually
Download angular-identity-map.js and include it on your page along with AngularJS
Add identity-map
to your module dependencies
angular;
Usage
Pre-requirements
All objects you want to map must have id
attribute and class_name
attribute in order to retrieve object identification pair. This pair (class_name, id) MUST be unique.
Both these behaviors can be customized. See Configuration chapter below.
All you need is .map
var myAppModule = angular; myAppModule
You can see and play around with live demo!
Others methods for those, who want to dig deeply into identity map
There are several methods available such as .get
, .set
, .detach
, .clear
etc.
Check out the source code for more information.
Configuration
identity-map stores and retrieves object using object type and object id.
By default object id is retrieved by calling id
attribute on an object. Object type retrieved by calling class_name
attribute on an object.
These functions are configurable in service provider:
Example:
app;
Please pay attention to couple requirements for these functions:
- Any pair of (entityType, entityId) values MUST represent one and only one entity object.
- Both functions accept one param which is guaranteed to be an object. If
entity
param is not a mappable object both functions MUST returnundefined
.
More examples can be found in spec/IdentityMap_spec.coffee
Integration with Restangular
"identity-map" can be integrated with Restangular easily. You just need to add simple response interceptor to Restangular stack:
angular;
Starting from now any Restangular response with "mappable" objects will be mapped automatically.
Development
Code quality is ensured with CoffeeScript, CoffeeLint, and Karma.
npm install -g grunt-clinpm install && bower installgrunt
Live Reloading
grunt karma:unit:start watch
Build
grunt dist
TODO
See issues list
Contribute
- Fork
- Code
- Submit PR