From 5dac61ec4093138a4e8488116557536ef76ea415 Mon Sep 17 00:00:00 2001 From: rigor789 Date: Sun, 12 Apr 2020 02:01:52 +0200 Subject: [PATCH 1/2] feat: setProperty on Observable --- 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 69bcfbc153..98a165b842 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 879138addf..f87684c82f 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 18f4699b91..bfeb80acee 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); } From 91044d64e5f1eca5ec574c64101637d9650f0b9a Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Mon, 13 Apr 2020 13:53:08 +0200 Subject: [PATCH 2/2] refactor: add missing whitespace Co-Authored-By: Vasil Trifonov --- nativescript-core/data/observable/observable.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nativescript-core/data/observable/observable.ts b/nativescript-core/data/observable/observable.ts index bfeb80acee..6b1eaed98c 100644 --- a/nativescript-core/data/observable/observable.ts +++ b/nativescript-core/data/observable/observable.ts @@ -66,7 +66,7 @@ export class Observable implements ObservableDefinition { this.notifyPropertyChange(name, value, oldValue); const specificPropertyChangeEventName = name + "Change"; - if(this.hasListeners(specificPropertyChangeEventName)) { + if (this.hasListeners(specificPropertyChangeEventName)) { const eventData = this._createPropertyChangeData(name, value, oldValue); eventData.eventName = specificPropertyChangeEventName; this.notify(eventData);