Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit 05f7842

Browse files
committed
docs update
1 parent ed69f03 commit 05f7842

File tree

1 file changed

+49
-71
lines changed

1 file changed

+49
-71
lines changed

README.md

Lines changed: 49 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ directive (class property) using HTML5' LocalStorage.
66
## What's new
77

88
Things that have been added in this fork:
9+
- added `.save()` method on returned object, used in specific cases to force save object changes
910
- support for `Array` methods that change its value (`push`, `pop` and `shift` to be exact)
10-
- `.save()` method on returned object, used in specific cases to force save object changes
11+
- now `WebStorageService.clear()` method removes items created by this repository only
12+
- storage key prefix (`angular2ws_` by default) can be customized by changing `WEBSTORAGE_CONFIG.prefix` property
1113

12-
## Use
14+
## Installation
1315

14-
1. Download the library using npm or github: `npm install --save angular2-localstorage`
16+
1. Download the library: `npm install --save git+https://github.com/zoomsphere/angular2-rest#master`
1517
2. Import the WebStorageModule in your app module:
1618
```typescript
17-
import {Component} from "angular2/core";
18-
import {WebStorageModule, LocalStorageService} from "angular2-localstorage";
19+
import {Component} from 'angular2/core';
20+
import {WebStorageModule, LocalStorageService} from 'angular2-localstorage';
1921

2022
@NgModule({
2123
import: [WebStorageModule]
@@ -25,38 +27,65 @@ Things that have been added in this fork:
2527
export class AppModule {}
2628
```
2729
30+
3. If you don't like the default key prefix (`angular2ws_`) or just don't want to use any, then in your `app.module.ts` file add the following:
31+
```typescript
32+
import {WEBSTORAGE_CONFIG} from 'angular2-localstorage';
33+
WEBSTORAGE_CONFIG.prefix = ''; // no prefix
34+
WEBSTORAGE_CONFIG.prefix = 'newPrefix_';
35+
```
36+
Note that it's the best to configure this right after installation, because the data saved under keys with previous prefix won't be automatically read anymore - to prevent that you can change keys of already stored data or override them manually.
2837
29-
3. Use the `LocalStorage` or `SessionStorage` decorator
38+
## How to use
39+
40+
1. Use the `@LocalStorage()` and/or `@SessionStorage()` decorator functions. Here is where the magic happens, decorated variables' values will be restored from the storage when you reload the site!
3041
```typescript
31-
import {LocalStorage, SessionStorage} from "angular2-localstorage/WebStorage";
42+
import {LocalStorage, SessionStorage} from 'angular2-localstorage/WebStorage';
3243

3344
class MySuperComponent {
34-
@LocalStorage() public lastSearchQuery:Object = {};
35-
@LocalStorage('differentLocalStorageKey') public lastSearchQuery:Object = {};
36-
@SessionStorage('itWillBeRemovedAfterBrowserClose') private lastSearchQueries: Array<{}> = [];
45+
// it will be stored under ${prefix}viewCounts name
46+
@LocalStorage() public viewCounts: number = 0;
47+
// this under name: ${prefix}differentLocalStorageKey
48+
@LocalStorage('differentLocalStorageKey') protected userName: string = '';
49+
// and this under ${prefix}itWillBeRemovedAfterBrowserClose in session storage
50+
@SessionStorage('itWillBeRemovedAfterBrowserClose') private previousUserNames: Array<string> = [];
51+
52+
mySuperMethod(): void {
53+
this.viewCounts++;
54+
this.userName = 'some name stored in localstorage';
55+
this.previousUserNames.push(this.userName);
56+
for (let userName of this.previousUserNames) {
57+
// do some stuff
58+
}
59+
}
3760
}
3861
```
3962
40-
4. Force save changes
41-
If you need to modify stored object in ways that can't be automatically handled (generally changing object properties or array elements using index signature),
42-
and don't want to use Service, then you can take advantage of `.save()` method to force save introduced changes. Example:
63+
2. **Force save changes.** If you need to modify stored object by not a direct assignment, then you can take advantage of `.save()` method to force save introduced changes. Example:
4364
```typescript
44-
import {LocalStorage, Webstorable} from 'angular2-localstorage';
65+
import {LocalStorage, SessionStorage, Webstorable} from 'angular2-localstorage';
4566

67+
// this is needed to satisfy Typescript type checking
4668
type WebstorableObject = Webstorable & Object; // save() method is declared in the Webstorable interface
69+
type WebstorableArray = Webstorable & Array<any>;
4770

4871
class MySuperComponent {
4972
@LocalStorage() someObject: WebstorableObject = <WebstorableObject>{};
73+
@SessionStorage() arrayOfSomethings: WebstorableArray = [0,1,2,3,4];
5074

51-
constructor() {
75+
mySuperMethod() {
5276
this.someObject.a = 1;
5377
this.someObject['b'] = 2;
5478
delete this.someObject['c'];
79+
for (let something of this.arrayOfSomethings) {
80+
something++;
81+
}
5582
// upper changes won't be saved without the lower line
5683
this.someObject.save();
84+
this.arrayOfSomethings.save();
5785
}
5886
}
5987
```
88+
Alternatively use `Local`(or Session)`StorageService` or make straight assignment by hand.
6089
6190
6291
**Note**: Define always a default value at the property you are using `@LocalStorage`.
@@ -65,59 +94,8 @@ and don't want to use Service, then you can take advantage of `.save()` method t
6594
6695
**Note**: Please don't store circular structures as this library uses JSON.stringify to encode before using LocalStorage.
6796
68-
## Examples
69-
70-
```typescript
71-
@Component({
72-
selector: 'app-login',
73-
template: `
74-
<form>
75-
<div>
76-
<input type="text" [(ngModel)]="username" placeholder="Username" />
77-
<input type="password" [(ngModel)]="password" placeholder="Password" />
78-
</div>
79-
80-
<input type="checkbox" [(ngModel)]="rememberMe" /> Keep me logged in
81-
82-
<button type="submit">Login</button>
83-
</form>
84-
`
85-
})
86-
class AppLoginComponent {
87-
//here happens the magic. `username` is always restored from the localstorage when you reload the site
88-
@LocalStorage() public username:string = '';
89-
90-
public password:string;
91-
92-
//here happens the magic. `rememberMe` is always restored from the localstorage when you reload the site
93-
@LocalStorage() public rememberMe:boolean = false;
94-
}
95-
```
96-
97-
98-
```typescript
99-
@Component({
100-
selector: 'admin-menu',
101-
template: `
102-
<div *ngFor="#menuItem of menuItems() | mapToIterable; #i = index">
103-
<h2 (click)="hiddenMenuItems[i] = !!!hiddenMenuItems[i]">
104-
{{i}}: {{category.label}}
105-
</h2>
106-
<div style="padding-left: 15px;" [hidden]="hiddenMenuItems[i]">
107-
<a href>Some sub menu item 1</a>
108-
<a href>Some sub menu item 2</a>
109-
<a href>Some sub menu item 3</a>
110-
</div>
111-
</div>
112-
`
113-
})
114-
class AdminMenuComponent {
115-
public menuItems = [{title: 'Menu1'}, {title: 'Menu2'}, {title: 'Menu3'}];
116-
117-
//here happens the magic. `hiddenMenuItems` is always restored from the localstorage when you reload the site
118-
@LocalStorage() public hiddenMenuItems:Array<boolean> = [];
119-
120-
//here happens the magic. `profile` is always restored from the sessionStorage when you reload the site from the current tab/browser. This is perfect for more sensitive information that shouldn't stay once the user closes the browser.
121-
@SessionStorage() public profile:any = {};
122-
}
123-
```
97+
### TODO
98+
- Try to automatically handle all data manipulations using [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)
99+
- Add tests
100+
101+
**Contributions are welcome!**

0 commit comments

Comments
 (0)