Skip to content

Commit dcb2bd4

Browse files
author
Alexander Vakrilov
authored
Merge pull request NativeScript#481 from NativeScript/can-go-back-api
RouterExtensions.canGoBack() method added
2 parents 62bf3de + 105b271 commit dcb2bd4

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

nativescript-angular/router/ns-location-strategy.ts

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export class NSLocationStrategy extends LocationStrategy {
113113

114114
}
115115

116+
canGoBack() {
117+
return this.states.length > 1;
118+
}
119+
116120
onPopState(fn: (_: any) => any): void {
117121
routerLog("NSLocationStrategy.onPopState");
118122
this.popStateCallbacks.push(fn);

nativescript-angular/router/router-extensions.ts

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export class RouterExtensions {
2828
this.locationStrategy.back();
2929
}
3030

31+
public canGoBack() {
32+
return this.locationStrategy.canGoBack();
33+
}
34+
3135
public backToPreviousPage() {
3236
this.frame.goBack();
3337
}

tests/app/tests/ns-location-strategy.ts

+14
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ describe('NSLocationStrategy', () => {
8080
assert.equal(strategy.path(), "/test");
8181
});
8282

83+
it("canGoBack() return false initially", () => {
84+
const strategy = initStrategy();
85+
86+
assert.isFalse(strategy.canGoBack(), "canGoBack() should reutrn false if there are no navigations");
87+
});
88+
89+
it("canGoBack() return true after navigation", () => {
90+
const strategy = initStrategy();
91+
92+
strategy.pushState(null, "test", "/test", null);
93+
94+
assert.isTrue(strategy.canGoBack(), "canGoBack() should reutrn true after navigation");
95+
});
96+
8397
it("back() calls onPopState", () => {
8498
const strategy = initStrategy();
8599
let popCount = 0;

0 commit comments

Comments
 (0)