consumerportal
Version:
mydna Custimised for you
83 lines (71 loc) • 2.13 kB
text/typescript
/// <reference path="../../includes.ts" />
module app {
export interface IAppStorage {
setItem(key: string, value: string);
getItem(key: string): string;
removeItem(key: string);
clear();
}
export class Storage implements IAppStorage {
private data: any = {};
private native: boolean;
constructor() {
try {
localStorage.setItem('mod', 'mod');
localStorage.removeItem('mod');
this.native = true;
} catch(e) {
this.native = false;
}
}
setItem(key: string, value: string) {
if (this.native) {
try {
localStorage.setItem(key, value);
} catch(e) {
this.native = false;
this.data[key] = value;
}
} else {
this.data[key] = value;
}
}
getItem(key: string) {
if (this.native) {
try {
return localStorage.getItem(key);
} catch(e) {
this.native = false;
return null;
}
} else {
return this.data.hasOwnProperty(key) ? this.data[key] : null;
}
}
removeItem(key: any) {
if (this.native) {
try {
localStorage.removeItem(key);
} catch(e) {
this.native = false;
}
} else {
delete this.data[key];
}
}
clear() {
if (this.native) {
try {
localStorage.clear();
} catch(e) {
this.native = false;
}
} else {
this.data = {};
}
}
}
angular
.module('app')
.service('appLocalStorage', Storage);
}