Skip to content

RouterExtensions.canGoBack() method added #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions nativescript-angular/router/ns-location-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ export class NSLocationStrategy extends LocationStrategy {

}

canGoBack() {
return this.states.length > 1;
}

onPopState(fn: (_: any) => any): void {
routerLog("NSLocationStrategy.onPopState");
this.popStateCallbacks.push(fn);
Expand Down
4 changes: 4 additions & 0 deletions nativescript-angular/router/router-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export class RouterExtensions {
this.locationStrategy.back();
}

public canGoBack() {
return this.locationStrategy.canGoBack();
}

public backToPreviousPage() {
this.frame.goBack();
}
Expand Down
14 changes: 14 additions & 0 deletions tests/app/tests/ns-location-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ describe('NSLocationStrategy', () => {
assert.equal(strategy.path(), "/test");
});

it("canGoBack() return false initially", () => {
const strategy = initStrategy();

assert.isFalse(strategy.canGoBack(), "canGoBack() should reutrn false if there are no navigations");
});

it("canGoBack() return true after navigation", () => {
const strategy = initStrategy();

strategy.pushState(null, "test", "/test", null);

assert.isTrue(strategy.canGoBack(), "canGoBack() should reutrn true after navigation");
});

it("back() calls onPopState", () => {
const strategy = initStrategy();
let popCount = 0;
Expand Down