angular2-localstorage
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -34,3 +34,2 @@ import {LocalStorageEmitter} from './LocalStorageEmitter'; | ||
var oldJSONValues = {}; | ||
console.log('init', storageValueJSON, storageValue); | ||
@@ -73,7 +72,6 @@ Object.defineProperty(target, decoratedPropertyName, { | ||
var currentValue = JSON.stringify(instance[decoratedPropertyName]); | ||
var oldJSONValue = storageValueJSON[this['_' + decoratedPropertyName + '_mapped']]; | ||
var oldJSONValue = oldJSONValues[this['_' + decoratedPropertyName + '_mapped']]; | ||
if (currentValue !== oldJSONValue) { | ||
storageValueJSON[this['_' + decoratedPropertyName + '_mapped']] = currentValue; | ||
oldJSONValues[this['_' + decoratedPropertyName + '_mapped']] = currentValue; | ||
localStorage.setItem(storageKey, currentValue); | ||
console.log('LocalStorage changed', instances.length, instance[decoratedPropertyName]); | ||
} | ||
@@ -83,2 +81,2 @@ } | ||
} | ||
} | ||
} |
@@ -6,6 +6,11 @@ import {Injectable, OnDestroy} from 'angular2/core'; | ||
protected static subscribed = {}; | ||
protected static subscribed = []; | ||
protected static ngZones:NgZone[] = []; | ||
public static register(ngZone:NgZone) { | ||
LocalStorageEmitter.subscribed[ngZone] = ngZone.onTurnDone.subscribe(() => { | ||
let index:number = LocalStorageEmitter.ngZones.indexOf(ngZone); | ||
if (index === -1) { | ||
index = LocalStorageEmitter.ngZones.push(ngZone) - 1; | ||
} | ||
LocalStorageEmitter.subscribed[index] = ngZone.onTurnDone.subscribe(() => { | ||
for (let callback of LocalStorageEmitter.subscribers) { | ||
@@ -24,3 +29,6 @@ callback(); | ||
public static unregister(ngZone:NgZone) { | ||
LocalStorageEmitter.subscribed[ngZone].unsubscribe(); | ||
let index:number = LocalStorageEmitter.ngZones.indexOf(ngZone); | ||
if (index >= 0) { | ||
LocalStorageEmitter.subscribed[index].unsubscribe(); | ||
} | ||
} | ||
@@ -35,3 +43,3 @@ } | ||
ngOnDestroy(){ | ||
ngOnDestroy() { | ||
LocalStorageEmitter.unregister(this.ngZone); | ||
@@ -43,8 +51,8 @@ } | ||
import {provide} from 'angular2/src/core/di'; | ||
import {ComponentRef} from 'angular2/core'; | ||
export function LocalStorageSubscriber(appPromise:Promise) { | ||
export function LocalStorageSubscriber(appPromise:Promise<ComponentRef>) { | ||
appPromise.then((bla) => { | ||
console.log('app booted', bla); | ||
console.log(bla.injector.resolveAndInstantiate(<Type>LocalStorageService)); | ||
bla.injector.resolveAndInstantiate(<Type>LocalStorageService); | ||
}); | ||
} |
{ | ||
"name": "angular2-localstorage", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Angular2 decorator to save and restore class properties automatically from LocalStorage.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,3 @@ # Angular2 @LocalStorage | ||
1. Download the library using npm or github: `npm install angular2-localStorage` | ||
1. Download the library using npm or github: `npm install angular2-localstorage` | ||
2. Register the LocalStorage in your boot.ts: | ||
@@ -15,3 +15,3 @@ ```typescript | ||
// register LocalStorage, this registers our change-detection. | ||
import {LocalStorageSubscriber} from 'angular2-localStorage/LocalStorageEmitter'; | ||
import {LocalStorageSubscriber} from 'angular2-localstorage/LocalStorageEmitter'; | ||
LocalStorageSubscriber(appPromise); | ||
@@ -21,3 +21,3 @@ ``` | ||
```typescript | ||
import {LocalStorage} from "angular2-localStorage/LocalStorage"; | ||
import {LocalStorage} from "angular2-localstorage/LocalStorage"; | ||
@@ -30,5 +30,8 @@ class MySuperComponent { | ||
*Note*: Define always a default value at the property you are using `@LocalStorage`. | ||
*Note*: The localStorage key is per default the property name. Define the first argument of `@LocalStorage` to set different one. | ||
**Note**: Define always a default value at the property you are using `@LocalStorage`. | ||
**Note**: The localStorage key is per default the property name. Define the first argument of `@LocalStorage` to set different one. | ||
**Note**: Please don't store circular structures as this library uses JSON.stringify to encode before using LocalStorage. | ||
## Examples | ||
@@ -46,3 +49,3 @@ | ||
<input type="checkbox" [(ngModel)]="keepLoggedIn" /> Keep me logged in | ||
<input type="checkbox" [(ngModel)]="rememberMe" /> Keep me logged in | ||
@@ -59,4 +62,4 @@ <button type="submit>Login</button | ||
//here happens the magic. `keepLoggedIn` is always restored from the localStorage when you reload the site | ||
@LocalStorage() public keepLoggedIn:boolean = false; | ||
//here happens the magic. `rememberMe` is always restored from the localStorage when you reload the site | ||
@LocalStorage() public rememberMe:boolean = false; | ||
} | ||
@@ -74,3 +77,3 @@ ``` | ||
</h2> | ||
<div style="padding-left: 15px;" [hidden]="!hiddenMenuItems[i]"> | ||
<div style="padding-left: 15px;" [hidden]="hiddenMenuItems[i]"> | ||
<a href>Some sub menu item 1</a> | ||
@@ -77,0 +80,0 @@ <a href>Some sub menu item 2</a> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8099
112
85