【问题标题】:Hash and backbone router哈希和骨干路由器
【发布时间】:2012-06-29 13:03:02
【问题描述】:

我应该如何使用路由器做到这一点?

var itemView = Marionette.ItemView.extend({

     events: {
         'click #loadPage': 'loadPage'
     },

     loadPage: function () {
         document.location.hash = '#tasks/' + this.model.get('id');
     }

});

【问题讨论】:

  • 你到底想达到什么目的?
  • 我想用 router.navigate 做同样的事情。或者也许是没用的,因为 Backbone.History 以我的方式记录它?

标签: backbone.js backbone-routing


【解决方案1】:

根据documentation,router.navigate 只是代理到 Backbone.history,它是全局的,因此您应该能够毫无问题地使用它:

Backbone.history.navigate("#tasks/", { trigger: true })

{trigger: true} 选项按预期将触发哈希更改,以便您的路由器可以在注册该路由时做出反应。

【讨论】:

    【解决方案2】:

    路由器实例必须具有“全局”变量。

    然后就这样调用那个变量:

    // yourRouter was defined in your main controller like this: 
    // var YourRouterClass = Backbone.Router.extend({ 
    // stuff of your own Router
    // });
    // var yourRouter = new YourRouterClass(); <- this is your global router instance
    yourRouter.navigate('tasks/'+this.model.get('id'), {trigger : true});
    

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多