Skip to content

Commit 36ae896

Browse files
committed
Fix: Set the binding context of a page automatically when navigated to.
Resolves #731
1 parent 0477c81 commit 36ae896

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

‎tests/app/ui/layouts/grid-layout-tests.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ class RemovalTrackingGridLayout extends GridLayout {
1717
public removedCols = 0;
1818

1919
public _onRowRemoved(itemSpec: ItemSpec, index: number) {
20-
console.log("_onRowRemoved");
2120
this.removedRows++;
2221
super._onRowRemoved(itemSpec, index);
2322
}
2423

2524
public _onColumnRemoved(itemSpec: ItemSpec, index: number) {
26-
console.log("_onColumnRemoved");
2725
this.removedCols++;
2826
super._onColumnRemoved(itemSpec, index);
2927
}

‎tests/app/ui/page/page-tests-common.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,31 @@ export function test_NavigateTo_WithContext() {
230230
TKUnit.assertNull(testPage.navigationContext, "Navigation context should be cleared on navigating back");
231231
}
232232

233+
//https://github.com/NativeScript/NativeScript/issues/731
234+
export function test_BindingContext_Becomes_NavigationContext_When_NavigatingTo() {
235+
let currentPage = frameModule.topmost().currentPage;
236+
let testPage: Page;
237+
let bindingContext;
238+
let pageFactory = function (): Page {
239+
testPage = new Page();
240+
testPage.on(pageModule.Page.navigatingToEvent, function (args: NavigatedData) {
241+
bindingContext = (<Page>args.object).bindingContext;
242+
});
243+
return testPage;
244+
};
245+
let navEntry = {
246+
create: pageFactory,
247+
context: "This is the navigation context",
248+
animated: false
249+
};
250+
let topFrame = frameModule.topmost();
251+
topFrame.navigate(navEntry);
252+
TKUnit.waitUntilReady(() => topFrame.currentPage !== null && topFrame.currentPage !== currentPage && testPage.isLayoutValid);
253+
helper.goBack();
254+
255+
TKUnit.assertEqual(bindingContext, navEntry.context, "The Page's bindingContext should be set automatically to the navigation context when navigating to.");
256+
}
257+
233258
export function test_FrameBackStack_WhenNavigatingForwardAndBack() {
234259
let testPage: Page;
235260
let pageFactory = function () {

‎tests/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"nativescript": {
77
"id": "org.nativescript.tests",
88
"tns-ios": {
9-
"version": "2.1.0"
9+
"version": "2.1.1"
1010
},
1111
"tns-android": {
1212
"version": "2.1.1"
@@ -23,4 +23,4 @@
2323
"lazy": "1.0.11",
2424
"typescript": "^1.8.10"
2525
}
26-
}
26+
}

‎tns-core-modules/ui/page/page-common.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,15 @@ export class Page extends ContentView implements dts.Page {
199199
};
200200
}
201201

202+
private _originalBindingContext: any;
202203
public onNavigatingTo(context: any, isBackNavigation: boolean) {
203204
this._navigationContext = context;
205+
206+
//https://github.com/NativeScript/NativeScript/issues/731
207+
if (!isBackNavigation && context && this.bindingContext !== context){
208+
this._originalBindingContext = this.bindingContext;
209+
this.bindingContext = context;
210+
}
204211
this.notify(this.createNavigatedData(Page.navigatingToEvent, isBackNavigation));
205212
}
206213

@@ -216,6 +223,12 @@ export class Page extends ContentView implements dts.Page {
216223
this.notify(this.createNavigatedData(Page.navigatedFromEvent, isBackNavigation));
217224

218225
this._navigationContext = undefined;
226+
227+
//https://github.com/NativeScript/NativeScript/issues/731
228+
if (isBackNavigation && this._originalBindingContext && this.bindingContext !== this._originalBindingContext){
229+
this.bindingContext = this._originalBindingContext;
230+
this._originalBindingContext = undefined;
231+
}
219232
}
220233

221234
public showModal(): Page {

0 commit comments

Comments
 (0)