From bb6a7107c9a50dad46deb6b536872a4a9ea2b649 Mon Sep 17 00:00:00 2001 From: Nedyalko Nikolov Date: Mon, 15 Aug 2016 17:07:14 +0300 Subject: [PATCH] Fixed issue when bind to bindingContext. --- tests/app/ui/bindable-tests.ts | 28 ++++++++++++++++++++++++++++ tns-core-modules/ui/core/bindable.ts | 17 +++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/tests/app/ui/bindable-tests.ts b/tests/app/ui/bindable-tests.ts index a00081a9e..bfbfaa780 100644 --- a/tests/app/ui/bindable-tests.ts +++ b/tests/app/ui/bindable-tests.ts @@ -1303,4 +1303,32 @@ export function test_Observable_from_nested_json_binds_correctly_when_upper_obje model.get("firstObject").set("secondObject", new observable.Observable({"dummyProperty": expectedValue})); TKUnit.assertEqual(obj.get("test"), expectedValue); +} + +export function test_BindingToBindingContextProperty_ShouldUseNewContext() { + let stackLayout = new stackLayoutModule.StackLayout(); + let label = new labelModule.Label(); + stackLayout.addChild(label); + + label.bind({ + sourceProperty: 'context', + targetProperty: 'bindingContext' + }); + + label.bind({ + sourceProperty: 'text', + targetProperty: 'text' + }); + + let testBindingContext = observable.Observable.fromJSONRecursive({ + context: { + text: 'Alabala' + } + }); + + stackLayout.bindingContext = testBindingContext; + + (testBindingContext).context.text = "Tralala"; + + TKUnit.assertEqual(label.text, "Tralala"); } \ No newline at end of file diff --git a/tns-core-modules/ui/core/bindable.ts b/tns-core-modules/ui/core/bindable.ts index afc8c976f..06c3df129 100644 --- a/tns-core-modules/ui/core/bindable.ts +++ b/tns-core-modules/ui/core/bindable.ts @@ -114,13 +114,22 @@ export class Bindable extends DependencyObservable implements definition.Bindabl } public _onBindingContextChanged(oldValue: any, newValue: any) { + let bindingContextBinding = this.bindings.get("bindingContext"); + if (bindingContextBinding) { + if (!bindingContextBinding.updating) { + bindingContextBinding.bind(newValue); + } + } + + let bindingContextSource = this.bindingContext; + this.bindings.forEach((binding, index, bindings) => { - if (!binding.updating && binding.sourceIsBindingContext) { + if (!binding.updating && binding.sourceIsBindingContext && binding.options.targetProperty !== "bindingContext") { if (trace.enabled) { - trace.write(`Binding ${binding.target.get()}.${binding.options.targetProperty} to new context ${newValue}`, trace.categories.Binding); + trace.write(`Binding ${binding.target.get()}.${binding.options.targetProperty} to new context ${bindingContextSource}`, trace.categories.Binding); } - if (!types.isNullOrUndefined(newValue)) { - binding.bind(newValue); + if (!types.isNullOrUndefined(bindingContextSource)) { + binding.bind(bindingContextSource); } else { binding.clearBinding(); }