所以,我试图制作一个具有 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})
关于javascript - Backbone 路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37170607/