【问题标题】:Backbone router with custom default route具有自定义默认路由的骨干路由器
【发布时间】:2012-09-03 14:18:42
【问题描述】:

我想在视图上启动一个主干应用程序,它已经由 Rails 渲染。

这是我的路由器代码

class App.Routers.Dashboard extends Backbone.Router
 routes:
  '': 'index'
  'locations/:id': 'showLocation'

  index: ->
     alert "Dashboard page"

  initialize: ->
    @route(/\/?/, 'index', @index);  

然后在渲染视图中,我启动应用程序

$ ->
 App.appRouter = new App.Routers.Dashboard()
 Backbone.history.start
  pushState: true
  root: "/dashboard"

但是,如果我打开页面

http://localhost:3000/dashboard

路由器没有进入“索引”状态。

我错过了什么吗?

【问题讨论】:

    标签: ruby-on-rails-3.2 backbone-routing


    【解决方案1】:

    默认路由 '' 只会在 URL 位于根目录时匹配。

    在您的示例中,“索引状态”将仅使用 http://localhost:3000/ 的 url 调用。

    推荐的方法是使用“splat”路由'*path': 'index'。因为这条路线实际上表示匹配任何东西,所以您需要确保它是最后定义的路线。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多