Tyler DeWitt
Tyler DeWitt

Reputation: 23576

Hello World Backbone Router Not Working

I'm trying to create the hello world of Backbone apps.

Here is my coffeescript file:

window.App =
  initialize: ->
    router = Backbone.Router.extend
      routes:
        '':'index'
      index: ->
        alert "hi, i am your working router"
    Backbone.history.start()


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

This is hooked to a rails app and visiting the root url (localhost:3000) does not trigger the alert.

Upvotes: 0

Views: 116

Answers (1)

FluffyJack
FluffyJack

Reputation: 1732

You were almost there. When you define a router you also need to create a new instance of it before it will actually listen to the Backbone history stuff.

window.App =
  initialize: ->

    # Define the class on window.App
    window.App.AppRouter = Backbone.Router.extend
      routes:
        '':'index'
      index: ->
        alert "hi, i am your working router"

    # Actually initialize an instance of it
    window.App.router = new window.App.AppRouter
    Backbone.history.start()


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

Upvotes: 1

Related Questions