feat: setProperty on Observable (#8521)

* feat: setProperty on Observable

* refactor: add missing whitespace

Co-Authored-By: Vasil Trifonov <v.trifonov@gmail.com>

Co-authored-by: Vasil Trifonov <v.trifonov@gmail.com>
This commit is contained in:
Igor Randjelovic
2020-04-14 08:57:29 +02:00
committed by GitHub
parent f0149dae7d
commit 7cf3c978fd
3 changed files with 26 additions and 2 deletions

View File

@@ -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 {

View File

@@ -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.
*/

View File

@@ -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);
}