使用Sammy.js取消路由而不会影响历史记录 [英] Cancel route using Sammy.js without affecting history

查看:71
本文介绍了使用Sammy.js取消路由而不会影响历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Sammy拦截所有路线更改,以便首先检查是否有待处理的操作.我已经使用sammy.before API进行了此操作,并且返回false取消了该路由.这样可以使用户停留在页面"上,但仍会更改浏览器地址栏中的哈希值,并将路由添加到浏览器的历史记录中.如果我取消了该路线,则不希望它在地址栏中也不在历史记录中,而是希望该地址保持不变.

I want to intercept all route changes with Sammy to first check if there is a pending action. I have done this using the sammy.before API and I return false to cancel the route. This keeps the user on the 'page' but it still changes the hash in the browsers address bar and adds the route to the browsers' history. If I cancel the route, I dont want it in the address bar nor history, but instead I expect the address to stay the same.

当前,要解决此问题,我可以调用window.history.back(yuk)返回历史记录中的原始位置,也可以调用sammy.redirect.两者都不尽如人意.

Currently, to get around this I can either call window.history.back (yuk) to go back to the original spot in the history or sammy.redirect. Both of which are less than ideal.

有没有办法让萨米真正取消路线,使其停留在当前路线/页面上,使地址栏保持原样,并且不添加到历史记录中?

Is there a way to make sammy truly cancel the route so it stays on the current route/page, leaves the address bar as is, and does not add to the history?

如果没有,是否还有另一个路由库可以做到这一点?

If not, is there another routing library that will do this?

sammy.before(/.*/, function () {
    // Can cancel the route if this returns false
    var response = routeMediator.canLeave();

if (!isRedirecting && !response.val) {
    isRedirecting = true;
    // Keep hash url the same in address bar
    window.history.back();
    //this.redirect('#/SpecificPreviousPage'); 
}
else {
    isRedirecting = false;
}
return response.val;
});

推荐答案

万一有人碰到这个,这就是我结束的地方.我决定使用sammy的context.setLocation功能来处理路线重置.

In case someone else hits this, here is where I ended up. I decided to use the context.setLocation feature of sammy to handle resetting the route.

sammy.before(/.*/, function () {
    // Can cancel the route if this returns false
    var
        context = this,
        response = routeMediator.canLeave();

    if (!isRedirecting && !response.val) {
        isRedirecting = true;
        toastr.warning(response.message); // toastr displays the message
        // Keep hash url the same in address bar
        context.app.setLocation(currentHash);
    }
    else {
        isRedirecting = false;
        currentHash = context.app.getLocation();
    }
    return response.val;
});

这篇关于使用Sammy.js取消路由而不会影响历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆