ngx-persisting-data
v0.0.5
Published
This library allows you to persist variables globally in your Angular 2.x projects <br>
Readme
ngx-persisting-data
This library allows you to persist variables globally in your Angular 2.x projects
Contents
##Installation Install this library via npm running the following command:
$ npm i ngx-persisting-data
##Basic Usage
Import the NgxPersistingDataModule into your module
import { NgxPersistingDataModule } from 'ngx-persisting-data'
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
NgxPersistingDataModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }Now, inject the NgxPersistingData class in your component constructor
constructor(private persistingData: NgxPersistingData) {}
ngOnInit(): void {
// Voilà, you can start using the library
this.persistingData.set({
namespace: 'bar',
context: StorageContext.Memory,
key: 'foo',
value: 123.45
});
alert(this.persistingData.get('foo', 'bar', StorageContext.Memory));
}##API Docs
NgxPersistingData
The persistence object
Type: class
#####Methods
| Name | Arguments | Returns | Description |
| ------------- | ------------- | ------------- | ------------- |
| get | (key: string, namespace: string, context: StorageContext) | any | Returns the current value of an existing item |
| getAsObservable | (key: string, namespace: string, context: StorageContext) | Observable<IPersistingVariable> | Returns an observable linked to an existing item |
| set | (conf: IPersistingDataConfigs) | Observable<IPersistingVariable> | Sets a new item |
| remove | (key: string, namespace: string, context: StorageContext)| void | Removes the specified item |
| clear | (context: StorageContext)| void | Removes the whole items in the specified storage context |
| ifExists | (key: string, namespace: string, context: StorageContext, existing: (value?: any) => any, nonExisting?: () => any)| void | A shortcut if statement that evaluates the existence of the specified item and executes the corresponding callback |
| bindProperty | (conf: IPersistingBindeablePropertyConfigs)| Observable<IPersistingVariable> | Overrides the get/set behavior of the specified component property and binds it to the storage |
| bindProperties | (conf: IPersistingBindeablePropertiesConfigs)| void | Overrides the get/set behavior of multiple component properties and binds them to the storage |
StorageContext
Contains 3 different storage instances
Type: enum
#####Properties
| Name | Description |
| ------------- | ------------- |
| Session | Corresponds to sessionStorage |
| Local | Corresponds to localStorage |
| Memory | Corresponds to a temporary object |
####IPersistingDataConfigs Contains the settings to store a variable
Type: interface
#####Properties
| Name | Type | Optional | Description |
| ------------- | ------------- | ------------- | ------------- |
| key | string | no | Variable name |
| context | StorageContext | yes | Selected instance to store the variable |
| namespace | string | no | Namespace of the variable |
| value | any or any[] | no | Variable value |
| expiration | number | yes | Indicates the time to expire the variable |
| onExpiration | (elem: IPersistingVariable) => any | yes | Callback that runs when the variable expires |
| onChange | (elem: IPersistingVariable) => any | yes | Callback that runs every time the variable changes of value |
####IPersistingVariable Contains the properties of a stored variable
Type: interface
#####Properties
| Name | Type | Optional | Description |
| ------------- | ------------- | ------------- | ------------- |
| key | string | no | Variable name |
| value | any or any[] | no | Variable value |
####IPersistingBindeablePropertyConfigs Contains the settings to bind a component property into the storage
Type: interface
#####Properties
| Name | Type | Optional | Description |
| ------------- | ------------- | ------------- | ------------- |
| property | string | no | Property name |
| context | string | yes | Selected instance to store the property |
| namespace | string | no | Namespace of the property |
| component | object | no | Component instance |
| onChange | (elem: IPersistingVaiable) => any | yes | Callback that runs every time the property changes of value |
####IPersistingBindeablePropertiesConfigs Contains the the settings to bind some component properties into the storage
Type: interface
#####Properties
| Name | Type | Optional | Description |
| ------------- | ------------- | ------------- | ------------- |
| properties | string[] | no | Property name |
| context | string | yes | Selected instance to store the property |
| namespace | string | no | Namespace of the property |
| component | object | no | Component instance |
| onChange | (elem: IPersistingVaiable) => any | yes | Callback that runs every time the property changes of value |
##License MIT
