mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
Merge pull request #2299 from NativeScript/nnikolov/BindingConverterFix
Fixed binding converter calls.
This commit is contained in:
@ -626,6 +626,36 @@ export function test_BindingToDictionaryAtAppLevel() {
|
|||||||
helper.buildUIAndRunTest(createLabel(), testFunc);
|
helper.buildUIAndRunTest(createLabel(), testFunc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function test_BindingConverterCalledEvenWithNullValue() {
|
||||||
|
var createLabel = function () {
|
||||||
|
var label = new labelModule.Label();
|
||||||
|
return label;
|
||||||
|
}
|
||||||
|
var pageViewModel = new observable.Observable();
|
||||||
|
var testPropertyValue = null;
|
||||||
|
var expectedValue = "collapsed";
|
||||||
|
pageViewModel.set("testProperty", testPropertyValue);
|
||||||
|
appModule.resources["converter"] = function(value) {
|
||||||
|
if (value) {
|
||||||
|
return "visible";
|
||||||
|
} else {
|
||||||
|
return "collapsed";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var testFunc = function (views: Array<viewModule.View>) {
|
||||||
|
var testLabel = <labelModule.Label>(views[0]);
|
||||||
|
testLabel.bind({ sourceProperty: "testProperty", targetProperty: "text", expression: "testProperty | converter" });
|
||||||
|
|
||||||
|
var page = <pageModule.Page>(views[1]);
|
||||||
|
page.bindingContext = pageViewModel;
|
||||||
|
|
||||||
|
TKUnit.assertEqual(testLabel.text, expectedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
helper.buildUIAndRunTest(createLabel(), testFunc);
|
||||||
|
}
|
||||||
|
|
||||||
export function test_UpdatingNestedPropertyViaBinding() {
|
export function test_UpdatingNestedPropertyViaBinding() {
|
||||||
var expectedValue1 = "Alabala";
|
var expectedValue1 = "Alabala";
|
||||||
var expectedValue2 = "Tralala";
|
var expectedValue2 = "Tralala";
|
||||||
|
@ -61,7 +61,7 @@ export class Bindable extends DependencyObservable implements definition.Bindabl
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if (!types.isNullOrUndefined(bindingSource)) {
|
// if (!types.isNullOrUndefined(bindingSource)) {
|
||||||
binding.bind(bindingSource);
|
binding.bind(bindingSource);
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,7 +119,11 @@ export class Bindable extends DependencyObservable implements definition.Bindabl
|
|||||||
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 ${newValue}`, trace.categories.Binding);
|
||||||
}
|
}
|
||||||
binding.bind(newValue);
|
if (!types.isNullOrUndefined(newValue)) {
|
||||||
|
binding.bind(newValue);
|
||||||
|
} else {
|
||||||
|
binding.clearBinding();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -222,16 +226,18 @@ export class Binding {
|
|||||||
|
|
||||||
source = this.sourceAsObject(source);
|
source = this.sourceAsObject(source);
|
||||||
|
|
||||||
|
let sourceValue;
|
||||||
|
|
||||||
if (!types.isNullOrUndefined(source)) {
|
if (!types.isNullOrUndefined(source)) {
|
||||||
this.source = new WeakRef(source);
|
this.source = new WeakRef(source);
|
||||||
this.sourceOptions = this.resolveOptions(source, this.sourceProperties);
|
this.sourceOptions = this.resolveOptions(source, this.sourceProperties);
|
||||||
|
|
||||||
let sourceValue = this.getSourcePropertyValue();
|
sourceValue = this.getSourcePropertyValue();
|
||||||
this.updateTarget(sourceValue);
|
this.updateTarget(sourceValue);
|
||||||
this.addPropertyChangeListeners(this.source, this.sourceProperties);
|
this.addPropertyChangeListeners(this.source, this.sourceProperties);
|
||||||
}
|
} else if (!this.sourceIsBindingContext) {
|
||||||
else {
|
sourceValue = this.getSourcePropertyValue();
|
||||||
this.updateTarget(source);
|
this.updateTarget(sourceValue ? sourceValue : source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -489,7 +495,7 @@ export class Binding {
|
|||||||
private getSourcePropertyValue() {
|
private getSourcePropertyValue() {
|
||||||
if (this.options.expression) {
|
if (this.options.expression) {
|
||||||
let changedModel = {};
|
let changedModel = {};
|
||||||
changedModel[bc.bindingValueKey] = this.source.get();
|
changedModel[bc.bindingValueKey] = this.source ? this.source.get() : undefined;
|
||||||
let expressionValue = this._getExpressionValue(this.options.expression, false, changedModel);
|
let expressionValue = this._getExpressionValue(this.options.expression, false, changedModel);
|
||||||
if (expressionValue instanceof Error) {
|
if (expressionValue instanceof Error) {
|
||||||
trace.write((<Error>expressionValue).message, trace.categories.Binding, trace.messageType.error);
|
trace.write((<Error>expressionValue).message, trace.categories.Binding, trace.messageType.error);
|
||||||
@ -516,6 +522,11 @@ export class Binding {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public clearBinding() {
|
||||||
|
this.clearSource();
|
||||||
|
this.updateTarget(undefined);
|
||||||
|
}
|
||||||
|
|
||||||
private updateTarget(value: any) {
|
private updateTarget(value: any) {
|
||||||
if (this.updating) {
|
if (this.updating) {
|
||||||
return;
|
return;
|
||||||
|
Reference in New Issue
Block a user