How to reload a route using hasher.js and crossroads.js?

不打扰是莪最后的温柔 提交于 2019-12-08 17:36:25

You are not changing anything with hasher.setHash("#/currentpage") because you are already on that page. So the change event never gets fired. You could probably just go:

$('a').on('click', function(e) {
    e.preventDefault();
    hasher.setHash('');
    hasher.setHash($(this).attr('href'));
});

But that would clutter the history, so you could force a reload on the click, if you make the loading method available:

var loadLogin = function(){
    $.ajax({
        .... your ajax settings ....
    });
};
crossroads.addRoute('/login', loadLogin);

$('a').on('click', function(e) {
    e.preventDefault();
    var newHash = $(this).attr('href');
    if(newHash === '/login' && hasher.getHash() === '/login') {
        loadLogin()
    } else {
        hasher.setHash(newHash);
    }
});

EDIT As @Patchesoft pointed out, crossroads has a property called ignoreState, which will force any request to go though the router function, even if on the current page - https://millermedeiros.github.io/crossroads.js/#crossroads-ignoreState

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!