diff --git a/apps/tests/ui/bindable-tests.ts b/apps/tests/ui/bindable-tests.ts index 914c22bd6..37a6b9b1c 100644 --- a/apps/tests/ui/bindable-tests.ts +++ b/apps/tests/ui/bindable-tests.ts @@ -1123,4 +1123,47 @@ export function test_BindingToPropertiesWithSameNamesSecondCase() { TKUnit.assertEqual(target1.get("targetProperty"), model.item.get("seconds")); TKUnit.assertEqual(target2.get("targetProp"), newValue); +} + +class RelatedPropsClass extends observable.Observable { + private _prop1: boolean; + private _prop2: string; + public get prop1(): boolean { + return this._prop1; + } + public set prop1(value: boolean) { + if (this._prop1 !== value) { + this._prop1 = value; + this.notifyPropertyChange("prop1", value); + } + } + + public get prop2(): string { + this.prop1 = !this._prop1; + this.notifyPropertyChange("prop2", this._prop2); + return this._prop2; + } + + public set prop2(value: string) { + if (this._prop2 !== value) { + this._prop2 = value; + this.notifyPropertyChange("prop2", value); + } + } +} + +export function test_BindingToRelatedProps() { + let model = new RelatedPropsClass(); + model.prop1 = false; + model.prop2 = "Alabala"; + + let target1 = new bindable.Bindable(); + target1.bind({sourceProperty: 'prop1', targetProperty: 'targetProp1'}, model); + + let target2 = new bindable.Bindable(); + target2.bind({sourceProperty: 'prop2', targetProperty: 'targetProp2'}, model); + + model.prop2 = "Tralala"; + + TKUnit.assertEqual(target2.get('targetProp2'), "Tralala"); } \ No newline at end of file diff --git a/ui/core/bindable.ts b/ui/core/bindable.ts index a646fa7ad..03aa093e4 100644 --- a/ui/core/bindable.ts +++ b/ui/core/bindable.ts @@ -414,8 +414,9 @@ export class Binding { } } - if (changedPropertyIndex > -1) { - let probablyChangedObject = this.propertyChangeListeners.get(parentProps); + // we need to do this only if nested objects are used as source and some middle object is changed. + if (changedPropertyIndex > -1 && changedPropertyIndex < sourcePropsLength - 1) { + var probablyChangedObject = this.propertyChangeListeners.get(parentProps); if (probablyChangedObject && probablyChangedObject !== data.object[sourceProps[changedPropertyIndex]]) { // remove all weakevent listeners after change, because changed object replaces object that is hooked for @@ -434,9 +435,9 @@ export class Binding { let newProps = sourceProps.slice(changedPropertyIndex + 1); // add new weakevent listeners - let newObject = data.object[sourceProps[changedPropertyIndex]] - if (typeof newObject === 'object') { - this.addPropertyChangeListeners(new WeakRef(data.object[sourceProps[changedPropertyIndex]]), newProps, parentProps); + var newObject = data.object[sourceProps[changedPropertyIndex]] + if (!types.isNullOrUndefined(newObject) && typeof newObject === 'object') { + this.addPropertyChangeListeners(new WeakRef(newObject), newProps, parentProps); } } } @@ -601,4 +602,4 @@ export class Binding { this.updating = false; } -} \ No newline at end of file +}