Skip to content

Commit 423066a

Browse files
author
Nedyalko Nikolov
authored
Merge pull request NativeScript#2299 from NativeScript/nnikolov/BindingConverterFix
Fixed binding converter calls.
2 parents 44be75a + adc8f01 commit 423066a

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

tests/app/ui/bindable-tests.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,36 @@ export function test_BindingToDictionaryAtAppLevel() {
626626
helper.buildUIAndRunTest(createLabel(), testFunc);
627627
}
628628

629+
export function test_BindingConverterCalledEvenWithNullValue() {
630+
var createLabel = function () {
631+
var label = new labelModule.Label();
632+
return label;
633+
}
634+
var pageViewModel = new observable.Observable();
635+
var testPropertyValue = null;
636+
var expectedValue = "collapsed";
637+
pageViewModel.set("testProperty", testPropertyValue);
638+
appModule.resources["converter"] = function(value) {
639+
if (value) {
640+
return "visible";
641+
} else {
642+
return "collapsed";
643+
}
644+
};
645+
646+
var testFunc = function (views: Array<viewModule.View>) {
647+
var testLabel = <labelModule.Label>(views[0]);
648+
testLabel.bind({ sourceProperty: "testProperty", targetProperty: "text", expression: "testProperty | converter" });
649+
650+
var page = <pageModule.Page>(views[1]);
651+
page.bindingContext = pageViewModel;
652+
653+
TKUnit.assertEqual(testLabel.text, expectedValue);
654+
}
655+
656+
helper.buildUIAndRunTest(createLabel(), testFunc);
657+
}
658+
629659
export function test_UpdatingNestedPropertyViaBinding() {
630660
var expectedValue1 = "Alabala";
631661
var expectedValue2 = "Tralala";

tns-core-modules/ui/core/bindable.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class Bindable extends DependencyObservable implements definition.Bindabl
6161
}
6262

6363
// if (!types.isNullOrUndefined(bindingSource)) {
64-
binding.bind(bindingSource);
64+
binding.bind(bindingSource);
6565
// }
6666
}
6767

@@ -119,7 +119,11 @@ export class Bindable extends DependencyObservable implements definition.Bindabl
119119
if (trace.enabled) {
120120
trace.write(`Binding ${binding.target.get()}.${binding.options.targetProperty} to new context ${newValue}`, trace.categories.Binding);
121121
}
122-
binding.bind(newValue);
122+
if (!types.isNullOrUndefined(newValue)) {
123+
binding.bind(newValue);
124+
} else {
125+
binding.clearBinding();
126+
}
123127
}
124128
});
125129
}
@@ -222,16 +226,18 @@ export class Binding {
222226

223227
source = this.sourceAsObject(source);
224228

229+
let sourceValue;
230+
225231
if (!types.isNullOrUndefined(source)) {
226232
this.source = new WeakRef(source);
227233
this.sourceOptions = this.resolveOptions(source, this.sourceProperties);
228234

229-
let sourceValue = this.getSourcePropertyValue();
235+
sourceValue = this.getSourcePropertyValue();
230236
this.updateTarget(sourceValue);
231237
this.addPropertyChangeListeners(this.source, this.sourceProperties);
232-
}
233-
else {
234-
this.updateTarget(source);
238+
} else if (!this.sourceIsBindingContext) {
239+
sourceValue = this.getSourcePropertyValue();
240+
this.updateTarget(sourceValue ? sourceValue : source);
235241
}
236242
}
237243

@@ -489,7 +495,7 @@ export class Binding {
489495
private getSourcePropertyValue() {
490496
if (this.options.expression) {
491497
let changedModel = {};
492-
changedModel[bc.bindingValueKey] = this.source.get();
498+
changedModel[bc.bindingValueKey] = this.source ? this.source.get() : undefined;
493499
let expressionValue = this._getExpressionValue(this.options.expression, false, changedModel);
494500
if (expressionValue instanceof Error) {
495501
trace.write((<Error>expressionValue).message, trace.categories.Binding, trace.messageType.error);
@@ -516,6 +522,11 @@ export class Binding {
516522
return null;
517523
}
518524

525+
public clearBinding() {
526+
this.clearSource();
527+
this.updateTarget(undefined);
528+
}
529+
519530
private updateTarget(value: any) {
520531
if (this.updating) {
521532
return;

0 commit comments

Comments
 (0)