How to refresh a page in a backbone application

后端 未结 13 2157
北恋
北恋 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 22:51

    In order to provide a reusable snippet of code I augmented the Backbone.View with a helper method to reuse it wherever we need to reload the same page despite the Backbone router limitations.

    In our app.js file or any other proper place

    Backbone.View = Backbone.View.extend({
        refreshPage: function(e){
            if(e.target.pathname){
                Backbone.history.navigate(e.target.pathname);
            }
            window.location.reload();           
        }
    });
    

    This way if we have a link such as

    Some text 
    

    In our View we would just need to bind the click event to that callback to leverage the refresh page feature, as far as the refreshPage helper will be available through the prototype chain

    events: {
        'click #whateverId': 'refreshPage', 
    }
    

    As far as we don't need to change the "a tag" it's a cleaner solution that will save some boilerplate coding

    Hope it helps

提交回复
热议问题