简体   繁体   English

骨架视图刷新

[英]Backbone view rendering on refesh

I'm having a problem with backbone routes and views. 我对主干路由和视图有疑问。 The views are properly rendered when I click on the link and the url is correct as well, but the problem is whenever I refresh the page, the url is still the same but the view goes back to the index view, wherein it's supposed to render the view under that route, which results to events not being properly handled. 当我单击链接并且URL也正确时,视图已正确呈现,但是问题是,每当刷新页面时,URL仍然相同,但是视图返回到索引视图,在该视图中应该呈现该路线下的视图,导致事件未得到正确处理。

here's the router: 这是路由器:

var MainRouter = Backbone.Router.extend({
    initialize: function() {
//        homeView.render();
    },
    routes: {
        '': 'index', 
        'register': 'register'
    }
});

var mainRouter = new MainRouter();

router.on('route:index', function() {
    if (htmlObj.hasClass('connected')) {
        if (htmlObj.hasClass('present')) {
            profileView.render();
        }
        homeView.render();
    }
});

router.on('route:register', function() {
    signUpView.render();
});

Backbone.history.start(); Backbone.history.start();

and then in the view events I set the trigger to true 然后在视图事件中将trigger设置为true

Backbone.history.navigate('#/signup', {trigger: true});

So it would run the function of that specific route. 因此它将运行该特定路由的功能。 I also tried using pushState and root but it has the same problem on refresh but this time it says that page not found. 我也尝试使用pushState and root但是刷新时存在相同的问题,但是这次它说找不到该页面。

Thanks 谢谢

Please try this code: 请尝试以下代码:

var MainRouter  = Backbone.Router.extend({
    routes: {
        "": "index",
        "register": "register",
    },
    index: function () {
        if (htmlObj.hasClass('connected')) {
            if (htmlObj.hasClass('present')) {
                profileView.render();
            }
            homeView.render();
        }
    },

    register: function() {
        signUpView.render();
    }

});

$(document).ready(function () {
    var r = new MainRouter();
    Backbone.history.start();
    r.navigate(window.location.hash.replace('#',''),{trigger: true});
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM