骨干应用程序未定义错误

时间:2013-07-07 03:32:42

标签: javascript ruby-on-rails-3 backbone.js

我刚刚开始使用主干和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()

1 个答案:

答案 0 :(得分:0)

声明了以下行?

players = new Dww2.Collections.Playermaps();

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

您可以通过在$(文档).ready函数

中创建Dww2.Collections.Playermaps对象来修复它