Files
NativeScript/e2e/appTestTs/app/observable-property-decorator.ts
Vasil Chimev 9b78a28f37 app
2019-11-20 18:26:45 +02:00

34 lines
981 B
TypeScript
Executable File

/**
This file defines a decorator you can use to enable two-way
binding on view-model properties.
For example:
import { ObservableProperty } from "../observable-property-decorator";
@ObservableProperty() myProperty: boolean = true;
Read more at https://www.nativescript.org/blog/nativescript-observable-magic-string-property-name-be-gone
**/
import { Observable } from 'tns-core-modules/data/observable';
export function ObservableProperty() {
return (target: Observable, propertyKey: string) => {
Object.defineProperty(target, propertyKey, {
get() {
return this["_" + propertyKey];
},
set(value) {
if (this["_" + propertyKey] === value) {
return;
}
this["_" + propertyKey] = value;
this.notifyPropertyChange(propertyKey, value);
},
enumerable: true,
configurable: true
});
};
}