Angular JS Service for managing service states (saving / restoring / invalidating)
Just do bower install angular-states
.
This service is meant to be used as a 'top layer' on the AngularJS app, for saving, restoring, invalidating services/factories states. As mentioned in several AngularJS good practices lists and posts, using the controller's $scope can lead to a lot of disadvantages (difficult to reuse and test, doesn't separate Model/Controller concers, thight coupling, etc).
However, when keeping the model on services/factories it's sometimes necessary to maintain the state persistent across utilizations, which leads to further validation and (possibily) expiration over time. This service aims to facilitate that task and to make each service state logic easier to apply.
Note: Each service/factory values to be persisted should be made public so that the service can work correctly.
Include the 'angularStates' module as a dependency on your app, and include the 'StatesService' when it's needed.
angular.module('yourApp', ['angularStates', '..'])
- angularStatesApp
-
Module of the Angular States.
- StatesService
Angular JS Service for managing service states (saving / restoring / invalidating)
Module of the Angular States.
Kind: global variable
Ngdoc: overview
Angular JS Service for managing service states (saving / restoring / invalidating)
Kind: global variable
Ngdoc: service
Registers a service instance on the persistence service
Kind: static method of StatesService
Throws:
Error
will throw if the current keyName is already in use
Param | Type | Description |
---|---|---|
serviceInstance | Object |
registered service instance - for updating its state afterwards |
keyName | String |
key used for mapping items the each service |
fields | Array |
service properties that should be saved. Note: the elements of this array can be strings (name of the field) or objects. In case of object, they must use the following schema { name: 'property name', // String value: defaultValue, // any type expire: 1231 // Integer (time of expiration in milliseconds for the current field) } |
Save the service state, based on the values mapped for the keyName provided
Kind: static method of StatesService
Param | Type | Description |
---|---|---|
keyName | String |
key used for mapping items to the service |
Recovers the service state
Kind: static method of StatesService
Param | Type | Description |
---|---|---|
keyName | String |
key used for mapping items to the service |
Resets the service state and clears the storage of that service
Kind: static method of StatesService
Param | Type | Description |
---|---|---|
keyName | String |
key used for mapping items to the service |
Clears the service state on the persistence layer
Kind: static method of StatesService
Param | Type | Description |
---|---|---|
keyName | String |
key used for mapping items to the service |