rails: backbone-on-rails gem-

2019-07-01 16:06发布

问题:

Trying to follow along with Ryan Bates Backbone.js tutorial to build a raffle application but i've already encountered problems with the first bit of code. In the init function of the application.js, he initialized the new instance of Raffler routes which is supposed to trigger the alert "Home Page" but I'm getting the following errors in Firebug that I don't understand

entries.js:5Uncaught ReferenceError: Raffler is not defined
entry.js:15Uncaught ReferenceError: Backbone is not defined
entries.js:23Uncaught ReferenceError: Backbone is not defined
index.js:17Uncaught ReferenceError: Backbone is not defined
application.js:7Uncaught SyntaxError: Invalid regular expression: missing /
raffler.js:9Uncaught TypeError: undefined is not a function

Any ideas how I can fix this?

javascripts/raffler/application.js

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

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

routers/entries.js

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

  index: ->
    alert "home page"

Update

After I ran the generator //= require_tree . was immediately after the require jquery_ujs which, i discovered (I think) is what caused some of the problems. However, I've now moved it to the bottom and am still getting this error

Raffler.Routers.Entries is not a constructor
[Break On This Error]   

new Raffler.Routers.Entries();

Application.js

    //= require jquery
    //= require jquery_ujs
    //= require underscore
    //= require backbone
    //
    //= require .//raffler
    //
    //= require_tree ../templates/
    //= require_tree .//models
    //= require_tree .//collections
    //= require_tree .//views
    //= require_tree .//routers
    //= require_tree .

回答1:

I had the very same problem. For me the solution was to remove the line

//= require_tree .

from the application.js file, because this line was before the lines for underscore and backbonejs.



回答2:

I found someone else who had the same problem and then got it to work. I copied his code (which was exactly the same as mine) into my file and now mine's working. Don't know why

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

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