我刚刚启动骨干网和JavaScript,并且在控制台中收到以下错误。

players = new Dww2.Collections.Playermaps()
ReferenceError: Dww2 is not defined


application.js

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

playermaps_router.js.coffee

class Dww2.Routers.Playermaps extends Backbone.Router

  routes:
    'playermaps': 'index'

  initialize: ->
    @collection = new Dww2.Collections.Playermaps()
    @collection.reset($('#container').data('playermaps'))

  index: ->
    view = new Dww2.Views.PlayermapsIndex(collection: @collection)
    $('#container').html(view.render().el)


playermaps.js.coffee

class Dww2.Collections.Playermaps extends Backbone.Collection

  url: '/playermaps'
  model: Dww2.Models.Playermap


dww2.js.coffee

window.Dww2 =
Models: {}
Collections: {}
Views: {}
Routers: {}
initialize: ->
  new Dww2.Routers.Playermaps()
  Backbone.history.start(pushState: true)

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

最佳答案

以下行在哪里声明?

players = new Dww2.Collections.Playermaps();


如果在页面的嵌入式players元素中声明了<script>变量,则很可能在您尝试为其分配新的Dww2.Collections.Playermaps时未加载dww2.js.coffee的内容。如果是这种情况,window.Dww2将不存在,并且将是不确定的。

您可以通过在$(document).ready函数中创建Dww2.Collections.Playermaps对象来修复此问题。

10-07 14:24