BackboneJs Router

BackboneJs Router

本文关键字:Router BackboneJs      更新时间:2023-09-26

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

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();

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

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

有关更多信息,请参阅Backbone History文档。