Skip to content

Instantly share code, notes, and snippets.

@vakrilov
Last active August 22, 2018 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vakrilov/efd6f98e547bf11fe8b2d7b368d18abb to your computer and use it in GitHub Desktop.
Save vakrilov/efd6f98e547bf11fe8b2d7b368d18abb to your computer and use it in GitHub Desktop.
view-state-utils
const viewStateKey = "__vs";
export function attachViewState<T>(attachTo: string, defaultValueFactory?: () => T) {
return (target: any, key: string) => {
const assureViewState = (obj) => {
if (typeof obj[attachTo][viewStateKey] === "undefined") {
// console.log("> creating default view sate");
obj[attachTo][viewStateKey] = defaultValueFactory();
}
}
// property getter
var getter = function () {
// console.log("> getter");
assureViewState(this);
return this[attachTo][viewStateKey]
};
// property setter
var setter = function (newVal) {
// console.log("> setter");
assureViewState(this);
this[attachTo][viewStateKey] = newVal;
};
// Delete property.
if (delete target[key]) {
// Create new property with getter and setter
Object.defineProperty(target, key, {
get: getter,
set: setter,
enumerable: true,
configurable: true
});
}
}
}
export function getViewState<T>(model: any): T {
return model[viewStateKey];
}
export function cleanViewState(model: any) {
return model[viewStateKey] = undefined;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment