Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Created January 7, 2016 07:01
Show Gist options
  • Save NetanelBasal/cbd0ade7aee29fe27167 to your computer and use it in GitHub Desktop.
Save NetanelBasal/cbd0ade7aee29fe27167 to your computer and use it in GitHub Desktop.
class Person {
@storage() public name: string;
@storage('sur', 'session') public surname: string;
constructor(name : string, surname : string) {
this.name = name;
this.surname = surname;
}
}
function storage(storageKey?: string, storageType?: string) {
const newKey = storageKey;
const stype = storageType;
return function logProperty(target: any, key: string) {
console.log('fdfdfd', storageKey);
var storageKey = newKey || key;
// property value
var _val = this[key];
// property getter
var getter = function () {
if(stype === 'session') {
if(localStorage.getItem(storageKey)) {
return localStorage.getItem(storageKey);
}
} else {
if(sessionStorage.getItem(storageKey)) {
return sessionStorage.getItem(storageKey);
}
}
};
// property setter
var setter = function (newVal) {
if(stype === 'session') {
sessionStorage.setItem(storageKey, newVal);
} else {
localStorage.setItem(storageKey, newVal);
}
};
// Delete property.
if (delete this[key]) {
// Create new property with getter and setter
Object.defineProperty(target, key, {
get: getter,
set: setter,
enumerable: true,
configurable: true
});
}
}
}
let person = new Person('dssd', 'dsds');
// console.log(person.name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment