Fixed issue when bindingContext is bound more than once.

This commit is contained in:
Nedyalko Nikolov
2015-04-30 13:48:52 +03:00
parent 315959a590
commit 2e4b2eacf2
10 changed files with 136 additions and 46 deletions

View File

@@ -10,6 +10,7 @@ import utils = require("utils/utils");
import pageModule = require("ui/page");
import stackLayoutModule = require("ui/layouts/stack-layout");
import bindingBuilder = require("ui/builder/binding-builder");
import labelModule = require("ui/label");
// <snippet module="ui/core/bindable" title="bindable">
// For information and examples how to use bindings please refer to special [**Data binding**](../../../../bindings.md) topic.
@@ -459,4 +460,17 @@ export var test_getBindableOptionsFromStringTwoParamsNamedFormat = function () {
TKUnit.assert(bindOptions.targetProperty === "targetBindProperty", "Expected: targetBindProperty, Actual: " + bindOptions.targetProperty);
TKUnit.assert(bindOptions.expression === "bindProperty * 2", "Expected: bindProperty * 2, Actual:" + bindOptions.expression);
TKUnit.assert(bindOptions.twoWay === true, "Expected: true, Actual: " + bindOptions.twoWay);
}
export var test_TwoElementsBindingToSameBindingContext = function () {
var testFunc = function (page: pageModule.Page) {
var upperStackLabel = <labelModule.Label>(page.getViewById("upperStackLabel"));
var label1 = <labelModule.Label>(page.getViewById("label1"));
var label2 = <labelModule.Label>(page.getViewById("label2"));
TKUnit.assertEqual(upperStackLabel.text, label1.text);
TKUnit.assertEqual(upperStackLabel.text, label2.text);
}
helper.navigateToModuleAndRunTest("./tests/ui/bindingContext_testPage", testFunc);
}

View File

@@ -0,0 +1,34 @@
import observableModule = require("data/observable");
import pageModule = require("ui/page");
class MainViewModel extends observableModule.Observable {
private _item: any;
constructor() {
super();
this.item = { Title: "Alabala" };
}
get item(): any {
return this._item;
}
set item(value: any) {
if (this._item !== value) {
this._item = value;
this.notifyPropertyChanged("item", value);
}
}
notifyPropertyChanged(propertyName: string, value: any) {
this.notify({ object: this, eventName: observableModule.Observable.propertyChangeEvent, propertyName: propertyName, value: value });
}
}
var viewModel = new MainViewModel();
export function pageLoaded(args: observableModule.EventData) {
var page = <pageModule.Page>args.object;
page.bindingContext = viewModel;
}

View File

@@ -0,0 +1,15 @@
<Page xmlns="http://www.nativescript.org/tns.xsd"
xmlns:g="components/grid-view/grid-view"
loaded="pageLoaded">
<StackLayout id="upperStack">
<Label id="upperStackLabel" text="{{ item.Title }}" />
<StackLayout id="firstStack" bindingContext="{{ item }}" orientation="horizontal">
<Label id="labelText1" text="1" />
<Label id="label1" text="{{ Title }}" />
</StackLayout>
<StackLayout id="secondStack" bindingContext="{{ item }}" orientation="horizontal">
<Label id="labelText2" text="2" />
<Label id="label2" text="{{ Title }}" />
</StackLayout>
</StackLayout>
</Page>

View File

@@ -143,6 +143,16 @@ export function buildUIAndRunTest(controlToTest, testFunction, pageCss?) {
}
}
export function navigateToModuleAndRunTest(moduleName, testFunction) {
navigateToModule(moduleName);
try {
testFunction(frame.topmost().currentPage);
}
finally {
goBack();
}
}
export function buildUIWithWeakRefAndInteract<T extends view.View>(createFunc: () => T, interactWithViewFunc?: (view: T) => void) {
var newPage: page.Page;
var testFinished = false;