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

Should be possible to add guards #26

Closed
finnsson opened this Issue Sep 5, 2012 · 0 comments

Comments

Projects
None yet
1 participant
@finnsson
Owner

finnsson commented Sep 5, 2012

Guards are methods that are run before the page navigation takes place and
that can stop the navigation from displaying a certain page.

Use the property guard: someMethod do apply the guard. The method
takes three parameters: page, route and callback. If the callback is called
the navigation takes place - otherwise it is stopped.

<div data-bind="page: {id: 'admin', guard: isLoggedIn}">
  This page is only accessible if the user is logged in.
</div>

where

isLoggedIn: function(page, route, callback) {
    if(viewModel.loggedIn()) {
        callback();
    } else {
        window.location.href = "login";
    }
}

Use cases are login, validating steps in state machines, etc.

The reason the guard takes a callback as third argument is simply because the guard might be async - accessing
a webserver for login details or asking if a valid shopping card exists etc.

@finnsson finnsson closed this in cdfe6a1 Sep 5, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment