Merge pull request #2586 from NativeScript/nnikolov/BindingToBindingContextIssue

Fixed issue when bind to bindingContext and other property.
This commit is contained in:
Nedyalko Nikolov
2016-08-16 09:13:15 +03:00
committed by GitHub
2 changed files with 41 additions and 4 deletions

View File

@@ -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;
(<any>testBindingContext).context.text = "Tralala";
TKUnit.assertEqual(label.text, "Tralala");
}

View File

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