From 17b6f1c5f67805aa8278c59a5fc03054abf3e2f3 Mon Sep 17 00:00:00 2001 From: Panayot Cankov Date: Fri, 7 Apr 2017 17:12:39 +0300 Subject: [PATCH] Property change notifications raise value and oldValue --- .../data/observable/observable.d.ts | 8 +++-- .../data/observable/observable.ts | 18 ++++------ .../ui/core/properties/properties.ts | 34 +++++++++++-------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/tns-core-modules/data/observable/observable.d.ts b/tns-core-modules/data/observable/observable.d.ts index 0dec03238..fd37e2f24 100644 --- a/tns-core-modules/data/observable/observable.d.ts +++ b/tns-core-modules/data/observable/observable.d.ts @@ -27,6 +27,10 @@ export interface PropertyChangeData extends EventData { * The new value of the property. */ value: any; + /** + * The previous value of the property. + */ + oldValue?: any; } /** @@ -129,7 +133,7 @@ export class Observable { /** * Notifies all the registered listeners for the property change event. */ - notifyPropertyChange(propertyName: string, newValue: any): void; + notifyPropertyChange(propertyName: string, value: any, oldValue?: any): void; /** * Checks whether a listener is registered for the specified event name. @@ -141,7 +145,7 @@ export class Observable { /** * This method is intended to be overriden by inheritors to provide additional implementation. */ - _createPropertyChangeData(name: string, value: any): PropertyChangeData; + _createPropertyChangeData(name: string, value: any, oldValue?: any): PropertyChangeData; _emit(eventNames: string); //@endprivate } diff --git a/tns-core-modules/data/observable/observable.ts b/tns-core-modules/data/observable/observable.ts index b1b5e5a9f..db8d1692c 100644 --- a/tns-core-modules/data/observable/observable.ts +++ b/tns-core-modules/data/observable/observable.ts @@ -40,13 +40,14 @@ export class Observable implements ObservableDefinition { public set(name: string, value: any) { // TODO: Parameter validation + const oldValue = this[name]; if (this[name] === value) { return; } const newValue = WrappedValue.unwrap(value); this[name] = newValue; - this.notifyPropertyChange(name, newValue); + this.notifyPropertyChange(name, newValue, oldValue); } public on(eventNames: string, callback: (data: EventData) => void, thisArg?: any) { @@ -125,21 +126,16 @@ export class Observable implements ObservableDefinition { } } - public notifyPropertyChange(name: string, newValue: any) { - this.notify(this._createPropertyChangeData(name, newValue)); + public notifyPropertyChange(name: string, value: any, oldValue?: any) { + this.notify(this._createPropertyChangeData(name, value, oldValue)); } public hasListeners(eventName: string) { return eventName in this._observers; } - public _createPropertyChangeData(name: string, value: any): PropertyChangeData { - return { - eventName: Observable.propertyChangeEvent, - propertyName: name, - object: this, - value: value - }; + public _createPropertyChangeData(propertyName: string, value: any, oldValue?: any): PropertyChangeData { + return { eventName: Observable.propertyChangeEvent, object: this, propertyName, value, oldValue }; } public _emit(eventNames: string) { @@ -195,7 +191,7 @@ class ObservableFromObject extends Observable { const newValue = WrappedValue.unwrap(value); this._map[name] = newValue; - this.notifyPropertyChange(name, newValue); + this.notifyPropertyChange(name, newValue, currentValue); } } diff --git a/tns-core-modules/ui/core/properties/properties.ts b/tns-core-modules/ui/core/properties/properties.ts index 868c3da7b..384da09a3 100644 --- a/tns-core-modules/ui/core/properties/properties.ts +++ b/tns-core-modules/ui/core/properties/properties.ts @@ -3,7 +3,7 @@ import * as definitions from "../view-base"; import { ViewBase } from "../view-base"; // Types. -import { WrappedValue } from "../../../data/observable"; +import { WrappedValue, PropertyChangeData } from "../../../data/observable"; import { Style } from "../../styling/style"; export { Style }; @@ -131,11 +131,12 @@ export class Property implements TypedPropertyDescriptor< } if (this.hasListeners(eventName)) { - this.notify({ + this.notify({ eventName: eventName, propertyName: name, object: this, - value: unboxedValue + value: unboxedValue, + oldValue: currentValue }); } @@ -163,11 +164,12 @@ export class Property implements TypedPropertyDescriptor< } if (owner.hasListeners(eventName)) { - owner.notify({ + owner.notify({ eventName: eventName, propertyName: name, object: owner, - value: value + value: value, + oldValue: currentValue }); } @@ -275,11 +277,12 @@ export class CoercibleProperty extends Property imp } if (this.hasListeners(eventName)) { - this.notify({ + this.notify({ eventName: eventName, propertyName: name, object: this, - value: unboxedValue + value: unboxedValue, + oldValue: currentValue }); } @@ -460,11 +463,12 @@ export class CssProperty implements definitions.CssProperty< } if (this.hasListeners(eventName)) { - this.notify({ + this.notify({ eventName: eventName, propertyName: name, object: this, - value: value + value: value, + oldValue: currentValue }); } @@ -529,11 +533,12 @@ export class CssProperty implements definitions.CssProperty< } if (this.hasListeners(eventName)) { - this.notify({ + this.notify({ eventName: eventName, propertyName: name, object: this, - value: value + value: value, + oldValue: currentValue }); } @@ -676,7 +681,7 @@ export class CssAnimationProperty { this.view[setNative](next); } if (this.hasListeners(eventName)) { - this.notify({ eventName, object: this, propertyName, value }); + this.notify({ eventName, object: this, propertyName, value, oldValue: prev }); } } } @@ -812,11 +817,12 @@ export class InheritedCssProperty extends CssProperty } if (this.hasListeners(eventName)) { - this.notify({ + this.notify({ eventName: eventName, propertyName: name, object: this, - value: newValue + value: newValue, + oldValue: currentValue }); }