mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Merge pull request #2586 from NativeScript/nnikolov/BindingToBindingContextIssue
Fixed issue when bind to bindingContext and other property.
This commit is contained in:
@@ -1304,3 +1304,31 @@ export function test_Observable_from_nested_json_binds_correctly_when_upper_obje
|
|||||||
|
|
||||||
TKUnit.assertEqual(obj.get("test"), 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");
|
||||||
|
}
|
||||||
@@ -114,13 +114,22 @@ export class Bindable extends DependencyObservable implements definition.Bindabl
|
|||||||
}
|
}
|
||||||
|
|
||||||
public _onBindingContextChanged(oldValue: any, newValue: any) {
|
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) => {
|
this.bindings.forEach((binding, index, bindings) => {
|
||||||
if (!binding.updating && binding.sourceIsBindingContext) {
|
if (!binding.updating && binding.sourceIsBindingContext && binding.options.targetProperty !== "bindingContext") {
|
||||||
if (trace.enabled) {
|
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)) {
|
if (!types.isNullOrUndefined(bindingContextSource)) {
|
||||||
binding.bind(newValue);
|
binding.bind(bindingContextSource);
|
||||||
} else {
|
} else {
|
||||||
binding.clearBinding();
|
binding.clearBinding();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user