未捕获的TypeError:无法调用未定义的方法'extend'

时间:2012-01-06 17:52:13

标签: ruby-on-rails-3.1 backbone.js coffeescript

我正在尝试使用CoffeeScript类来扩展Backbone.Model。我构建了一个全新的rails 3.1应用程序,创建了一个具有3个属性的'Stone'脚手架,并将一个Todos.coffee示例的片段修补到stones.js.coffee中。我在app / assets / javascripts文件夹中有backbone.js和underscore.js。当我在Chrome Java控制台下运行时,我在控制台日志中收到上面的消息。有什么想法吗?

实际代码如下:

$ -> 

  class Todo extends Backbone.Model
    # Default attributes for the todo.
    defaults:
     content: "empty todo..."
     done: false

    # Ensure that each todo created has `content`.
    initialize: ->
      if !@get("content")
      @set({ "content": @defaults.content })

    # Toggle the `done` state of this todo item.
    toggle: ->
      @save({ done: !@get("done") })

    # Remove this Todo from *localStorage* and delete its view.
    clear: ->
      @destroy()
      @view.remove()

正在使用的application.js是由Rails 3.1生成的。我复制了Todos github repo中的backbone.js和underscore.js,https://github.com/JasonGiedymin/backbone-todojs-coffeescript

1 个答案:

答案 0 :(得分:16)

问题只是在underscore.js之后加载了backbone.js,当时它是必须加载的先决条件。 (请注意Backbone.js source中它立即设置var _ = root._,所以即使稍后声明了全局_,它也不会从Backbone的范围中看到。)Sprockets会在资源目录中加载JS文件按字母顺序默认排列。

您可以使用Sprockets:Put

来解决此问题
//= require underscore.js

//= require_tree .

确保首先加载它。

相关问题