From 7cf3c978fdece0b38d5b22598597a87d068390b1 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Tue, 14 Apr 2020 08:57:29 +0200 Subject: [PATCH] feat: setProperty on Observable (#8521) * feat: setProperty on Observable * refactor: add missing whitespace Co-Authored-By: Vasil Trifonov Co-authored-by: Vasil Trifonov --- api-reports/NativeScript.api.md | 7 +++++-- .../data/observable/observable.d.ts | 5 +++++ nativescript-core/data/observable/observable.ts | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/api-reports/NativeScript.api.md b/api-reports/NativeScript.api.md index 69bcfbc15..98a165b84 100644 --- a/api-reports/NativeScript.api.md +++ b/api-reports/NativeScript.api.md @@ -1503,6 +1503,8 @@ export class Observable { removeEventListener(eventNames: string, callback?: any, thisArg?: any); set(name: string, value: any): void; + + setProperty(name: string, value: any): void; //@endprivate } @@ -2425,10 +2427,11 @@ export class TabViewItem extends ViewBase { // @public export interface TapGestureEventData extends GestureEventData { - getPointerCount(): number; + getPointerCount(): number; getX(): number; getY(): number; -} + + } // @public export interface Template { diff --git a/nativescript-core/data/observable/observable.d.ts b/nativescript-core/data/observable/observable.d.ts index 879138add..f87684c82 100644 --- a/nativescript-core/data/observable/observable.d.ts +++ b/nativescript-core/data/observable/observable.d.ts @@ -129,6 +129,11 @@ export class Observable { */ set(name: string, value: any): void; + /** + * Updates the specified property with the provided value and raises a property change event and a specific change event based on the property name. + */ + setProperty(name: string, value: any): void; + /** * Gets the value of the specified property. */ diff --git a/nativescript-core/data/observable/observable.ts b/nativescript-core/data/observable/observable.ts index 18f4699b9..6b1eaed98 100644 --- a/nativescript-core/data/observable/observable.ts +++ b/nativescript-core/data/observable/observable.ts @@ -57,6 +57,22 @@ export class Observable implements ObservableDefinition { this.notifyPropertyChange(name, newValue, oldValue); } + public setProperty(name: string, value: any) { + const oldValue = this[name]; + if (this[name] === value) { + return; + } + this[name] = value; + this.notifyPropertyChange(name, value, oldValue); + + const specificPropertyChangeEventName = name + "Change"; + if (this.hasListeners(specificPropertyChangeEventName)) { + const eventData = this._createPropertyChangeData(name, value, oldValue); + eventData.eventName = specificPropertyChangeEventName; + this.notify(eventData); + } + } + public on(eventNames: string, callback: (data: EventData) => void, thisArg?: any) { this.addEventListener(eventNames, callback, thisArg); }