Backbone.js: Router callback not reached

眉间皱痕 提交于 2019-12-02 18:13:19

问题


I'm having trouble getting a simple App example to route. I'm using the backbone-on-rails gem.

Here's my 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])

The router never reaches the index callback and the View is never rendered.

Here's the HTML Page that is rendered by Rails:

<!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>

What do I need to do to properly instantiate a Backbone Router and get it to route to my Views?


回答1:


Looks like you just need to call Backbone.history.start rather than simply reference it. This just references the function:

Backbone.history.start

This calls it:

Backbone.history.start()

The function-calling parentheses are only optional when you supply some arguments.

Demo: http://jsfiddle.net/ambiguous/hUZUV/



来源:https://stackoverflow.com/questions/18448180/backbone-js-router-callback-not-reached

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!