0

我无法获得一个简单的应用程序示例来路由。我正在使用backbone-on-rails gem。

这是我的 App.js.coffee:

    window.App =
      Models: {}
      Collections: {}
      Views: {}
      Routers: {}

 $(document).ready ->
    MyRouter = Backbone.Router.extend(
       routes:
        '' : 'index'

        index: ->
            console.log("Inside router")
            new App.Views.HomeIndex()
    )
    router = new MyRouter
    Backbone.history.start
    console.log(router.routes[Backbone.history.fragment])

路由器永远不会到达索引回调并且永远不会渲染视图。

这是由 Rails 呈现的 HTML 页面:

<!DOCTYPE html>
<html>
<head>
  <title>App</title>
  <link href="/assets/application.css?body=1" media="screen" rel="stylesheet" />
  <script src="/assets/jquery.js?body=1"></script>
<script src="/assets/jquery_ujs.js?body=1"></script>
<script src="/assets/underscore.js?body=1"></script>
<script src="/assets/backbone.js?body=1"></script>
<script src="/assets/app.js?body=1"></script>
<script src="/assets/homes/index.js?body=1"></script>
<script src="/assets/models/home.js?body=1"></script>
<script src="/assets/collections/homes.js?body=1"></script>
<script src="/assets/views/homes/homes_index.js?body=1"></script>
<script src="/assets/routers/homes_router.js?body=1"></script>
<script src="/assets/routers/homes_routers.js?body=1"></script>
<script src="/assets/application.js?body=1"></script>
  <meta content="authenticity_token" name="csrf-param" />
<meta content="sA25aKKc/j2EJL6k8J0gm8SxGU2mHRhH8Sb6Sye81Ac=" name="csrf-token" />
</head>
<body>

<div id="app"></div>

</body>
</html>

我需要做什么才能正确实例化主干路由器并让它路由到我的视图?

4

1 回答 1

0

看起来您只需要调用Backbone.history.start而不是简单地引用它。这只是引用函数:

Backbone.history.start

这叫它:

Backbone.history.start()

仅当您提供一些参数时,函数调用括号才是可选的。

演示:http: //jsfiddle.net/ambiguous/hUZUV/

于 2013-08-26T16:28:33.273 回答