Routing (prevent / force)


#1

How do I prevent a route change and then, at the same time, move to another route?

In my case there is no user yet registered so the only available route would be the user registration. So no matter where the user tries to navigate the application should take the user to the user registration route.

I came across can-interrupt but I don’t know if it is still the recommended mechanism.


#2

Are you using pushstate? There are internal setup and tear down methods you could use to turn off the routing connection. There’s also the replaceState methods that make certain URLs use replace instead of pushState.

I’ll have time for a better explanation Monday.


#3

Thanks Justin,

I am not using pushstate. Just the vanilla can-route. I’ll take a look at pushstate in the meantime.

I have a workaround that involves overriding the route:

var RouteData = DefineMap.extend({
    resource: {
        type:'string',
        value: ''
    },
    action: {
        type:'string',
        value: ''
    },
    id: {
        type:'string',
        value: ''
    },
    full: {
        get: function () {
            return this.resource + (!!this.id ? `/${this.id}` : '') + (!!this.action ? `/${this.action}` : '');
        }
    }
});

var routeData = new RouteData();

routeData.on('full', function(ev, newVal, oldVal){
    if (!security.isUserRequired || (this.resource === 'user' && this.action === 'register')) {
        return;
    }

    this.update({resource: 'user', action: 'register'}, true);
});

Probably not ideal but it gets the job done until I have a better understanding.

I know you guys are working on some guidance around routing and I reckon it would be quite helpful to many people.