Fix: Set the binding context of a page automatically when navigated to.

Resolves #731
This commit is contained in:
Rossen Hristov
2016-07-18 13:38:57 +03:00
parent 0477c81dd5
commit 36ae896933
4 changed files with 40 additions and 4 deletions

View File

@@ -17,13 +17,11 @@ class RemovalTrackingGridLayout extends GridLayout {
public removedCols = 0;
public _onRowRemoved(itemSpec: ItemSpec, index: number) {
console.log("_onRowRemoved");
this.removedRows++;
super._onRowRemoved(itemSpec, index);
}
public _onColumnRemoved(itemSpec: ItemSpec, index: number) {
console.log("_onColumnRemoved");
this.removedCols++;
super._onColumnRemoved(itemSpec, index);
}

View File

@@ -230,6 +230,31 @@ export function test_NavigateTo_WithContext() {
TKUnit.assertNull(testPage.navigationContext, "Navigation context should be cleared on navigating back");
}
//https://github.com/NativeScript/NativeScript/issues/731
export function test_BindingContext_Becomes_NavigationContext_When_NavigatingTo() {
let currentPage = frameModule.topmost().currentPage;
let testPage: Page;
let bindingContext;
let pageFactory = function (): Page {
testPage = new Page();
testPage.on(pageModule.Page.navigatingToEvent, function (args: NavigatedData) {
bindingContext = (<Page>args.object).bindingContext;
});
return testPage;
};
let navEntry = {
create: pageFactory,
context: "This is the navigation context",
animated: false
};
let topFrame = frameModule.topmost();
topFrame.navigate(navEntry);
TKUnit.waitUntilReady(() => topFrame.currentPage !== null && topFrame.currentPage !== currentPage && testPage.isLayoutValid);
helper.goBack();
TKUnit.assertEqual(bindingContext, navEntry.context, "The Page's bindingContext should be set automatically to the navigation context when navigating to.");
}
export function test_FrameBackStack_WhenNavigatingForwardAndBack() {
let testPage: Page;
let pageFactory = function () {

View File

@@ -6,7 +6,7 @@
"nativescript": {
"id": "org.nativescript.tests",
"tns-ios": {
"version": "2.1.0"
"version": "2.1.1"
},
"tns-android": {
"version": "2.1.1"
@@ -23,4 +23,4 @@
"lazy": "1.0.11",
"typescript": "^1.8.10"
}
}
}

View File

@@ -199,8 +199,15 @@ export class Page extends ContentView implements dts.Page {
};
}
private _originalBindingContext: any;
public onNavigatingTo(context: any, isBackNavigation: boolean) {
this._navigationContext = context;
//https://github.com/NativeScript/NativeScript/issues/731
if (!isBackNavigation && context && this.bindingContext !== context){
this._originalBindingContext = this.bindingContext;
this.bindingContext = context;
}
this.notify(this.createNavigatedData(Page.navigatingToEvent, isBackNavigation));
}
@@ -216,6 +223,12 @@ export class Page extends ContentView implements dts.Page {
this.notify(this.createNavigatedData(Page.navigatedFromEvent, isBackNavigation));
this._navigationContext = undefined;
//https://github.com/NativeScript/NativeScript/issues/731
if (isBackNavigation && this._originalBindingContext && this.bindingContext !== this._originalBindingContext){
this.bindingContext = this._originalBindingContext;
this._originalBindingContext = undefined;
}
}
public showModal(): Page {