gpt4 book ai didi

javascript - Backbone 路由器

转载 作者:行者123 更新时间:2023-12-02 14:39:10 28 4
gpt4 key购买 nike

所以,我试图制作一个具有 Backbone 的示例应用程序,但我无法使路由器工作,对此有什么想法吗?谢谢。这是我的路由器

var Workspace = Backbone.Router.extend({
routes:{
"":"mainView",
"event":"event"
},
initialize: function(){
console.log('Hai');
//view = new MainView();
//this.render(view);
//alert( this.routes[Backbone.history.getFragment()] );
},
mainView: function(){
console.log('Load main view');
view = new MainView();
this.render(view);
},
event: function(){
view = new EventView();
this.render(view);
},
render: function(view){
if(this.currentView){
this.currentView.destroy();
}
view.render();
this.currentView = view;
}
});
var router = new Workspace();

https://jsfiddle.net/ypuh8yqv/

路由器被初始化,因为我看到了 hai 日志

最佳答案

您必须启动 Backbone 的历史记录模块。创建路由器实例后添加此行:

Backbone.history.start();

如果您想使用推送状态历史记录(因此网址中没有哈希值),您可以启用推送状态:

Backbone.history.start({pushState: true})

了解更多,see the Backbone History docs .

关于javascript - Backbone 路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37170607/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com