Skip to content

Commit 6f4e687

Browse files
authored
Merge pull request NativeScript#2467 from NativeScript/page-binding-context
Fix: Set the binding context of a page automatically when navigated to.
2 parents 0477c81 + 0dcb0c9 commit 6f4e687

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as fileSystemModule from "file-system";
99
import * as frameModule from "ui/frame";
1010
import proxy = require("ui/core/proxy");
1111
import keyframeAnimation = require("ui/animation/keyframe-animation");
12+
import types = require("utils/types");
1213

1314
let fs: typeof fileSystemModule;
1415
function ensureFS() {
@@ -201,6 +202,11 @@ export class Page extends ContentView implements dts.Page {
201202

202203
public onNavigatingTo(context: any, isBackNavigation: boolean) {
203204
this._navigationContext = context;
205+
206+
//https://github.com/NativeScript/NativeScript/issues/731
207+
if (!isBackNavigation && !types.isNullOrUndefined(context)){
208+
this.bindingContext = context;
209+
}
204210
this.notify(this.createNavigatedData(Page.navigatingToEvent, isBackNavigation));
205211
}
206212

0 commit comments

Comments
 (0)