Backbone.js:未达到路由器回调

时间:2013-08-26 15:58:03

标签: backbone.js coffeescript backbone-routing

我无法通过简单的App示例进行路由。我正在使用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])

路由器永远不会到达索引回调,并且永远不会呈现View。

这是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>

如何正确实例化Backbone路由器并让它路由到我的视图,我需要做什么?

1 个答案:

答案 0 :(得分:0)

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

Backbone.history.start

这称之为:

Backbone.history.start()

只有在提供一些参数时,函数调用括号才是可选的。

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