Question

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?

Was it helpful?

Solution

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top