opitzconsulting / jquery-mobile-angular-adapter

jquery mobile angular adapter
MIT License
517 stars 114 forks source link

Routing redirect without pageload #116

Closed davidd4 closed 11 years ago

davidd4 commented 11 years ago

Is there any way to redirect a route before the page is loaded (shown)? I'd like to create pages where a conditional redirect sholud be done. If a condition fails the user will be redirected to another page. Currently I do it in onActivate. The page is loaded first (presented for UI), then onActivate is called, where I can make the redirection. So the user can see the switch between the pages, which is not the desired effect. If I use $rootScope.$on("$routeChangeStart", ... I have the same effect.

marcorinck commented 11 years ago

If you decide in a controller function for a link/button which route to go to, this should work.

When you do it in onActivate the route change already has happended and its too late for a (seamless) redirect.

davidd4 commented 11 years ago

What I really want is not to make the decision (via checking the proper conditions) on the caller side (e.g. pressing a button/link). The caller is a separate controller, which has nothing to do to the target route. The controller of the target route should make the checks and decisions about the redirection.

tbosch commented 11 years ago

Hi, sorry, it's not possible that the controller of the target page is doing the decision, as we don't know the controller until the target page is loaded (which you want to make conditional). Thanks @romulus23 for correctly commenting this.

The only possibilities are to either use a redirect function in a route (see http://docs.angularjs.org/api/ng.$routeProvider) , or a controller function on the page that starts the route.

Closing this, as this is not related to the adapter but generally to angular.

Tobias

tbosch commented 11 years ago

Hi, another idea for a custom redirect would be to catch the $locationChangeStart event, prevent the default action and change $location to a new value:

$rootScope.$on('$locationChangeStart', function(event, newLocation) {
    if (newLocation==='someUrlToReactTo') {
        event.preventDefault();
        $rootScope.evalAsync(function() {
             $location.url('someNewUrl');
        });
    }
});