meleyal / backbone-on-rails

A simple gem for using Backbone.js with Rails.
MIT License
808 stars 125 forks source link

Route callback not executing #51

Closed ddrake closed 11 years ago

ddrake commented 11 years ago

Hi, I'm walking through the Railscasts backbone #1 episode, but using Rails 4.0.0 and Ruby 2.0. At this point, requesting the root should display an alert dialog with message 'home page', but I'm not getting that. Here's my code:

entries.router.js.coffee

class Raffler.Routers.Entries extends Backbone.Router
  routes:
    '': 'index'

    index: ->
      alert 'home page'

raffler.js.coffee

window.Raffler =
  Models: {}
  Collections: {}
  Views: {}
  Routers: {}
  initialize: -> 
    new Raffler.Routers.Entries()
    Backbone.history.start()

$(document).ready ->
  Raffler.initialize()

In the rails application, I'm rooting to "main#index" which is correctly rendering the view with the div showing "loading", but I'm not getting the alert dialog.

I'm not seeing any javascript errors in either Firefox 12 or Chrome ver. 28.0. When I step through the code, I can see that Backbone.history.start() is returning true, indicating that the url matched the route.

I removed the turbolinks gem from my bundle (and also from application.js and the layout), but that doesn't seem to have been the issue.

Can anyone spot what I'm doing wrong or recommend some way that I can debug this?

ddrake commented 11 years ago

I noticed that the railscast was based on Backbone version 0.9.1, while the current version of this gem is using version 1.0.0, but I've been through the backbone changelog and looked at the tests at github and still can't see where I'm going wrong...

ddrake commented 11 years ago

Ok, you can close this. I figured it out. The problem was in entries.router.js.coffee; my indentation was wrong. It needs to be like this instead:

class Raffler.Routers.Entries extends Backbone.Router
  routes:
    '': 'index'

  index: ->
    alert 'home page'

I was setting index as a property of the routes object.