How to refresh a page in a backbone application

后端 未结 13 2179
北恋
北恋 2021-01-30 22:32

I am using backbone to build my web app.

Currently I am facing an issue whereby if I am on the home page, I am unable to refresh the same page by just clicking on the \'

13条回答
  •  不知归路
    2021-01-30 23:01

    Looking at the backbone.js source, it seems as though this is not possible by default, since it only responds to a url change. And since clicking the same link, you would not trigger a change event.

    Code in Backbone.History:

    $(window).bind('hashchange', this.checkUrl);
    

    You'll need to handle a non-change yourself. Here's what I did:

    $('.nav-links').click(function(e) {
        var newFragment = Backbone.history.getFragment($(this).attr('href'));
        if (Backbone.history.fragment == newFragment) {
            // need to null out Backbone.history.fragement because 
            // navigate method will ignore when it is the same as newFragment
            Backbone.history.fragment = null;
            Backbone.history.navigate(newFragment, true);
        }
    });
    

提交回复
热议问题