未捕获的类型错误:无法调用方法“扩展”未定义的
我正在尝试获取 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 存储库复制了backbone.js 和 underscore.js, https://github.com /JasonGiedymin/backbone-todojs-coffeescript
I am trying to get a CoffeeScript class to extend a Backbone.Model. I built a brand new rails 3.1 app, created a scaffold of 'Stone', with 3 attributes, and patched a snippet of the Todos.coffee example into stones.js.coffee. I have both backbone.js and underscore.js in the app/assets/javascripts folder. When I run this under the Chrome Java console, I get the message above in the console log. Any ideas?
Actual code follows:
$ ->
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()
The application.js being used is what was generated by Rails 3.1. I copied the backbone.js and underscore.js from the Todos github repo, https://github.com/JasonGiedymin/backbone-todojs-coffeescript
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题很简单,
underscore.js
在backbone.js
之后加载,而它是必须在之前加载的先决条件。 (请注意 Backbone.js 源 它设置了var _ = root._
立即生效,因此即使稍后声明全局_
,它在 Backbone 的作用域中也是不可见的。)Sprockets 默认按字母顺序加载资产目录中的 JS 文件。您可以使用 Sprockets: Put
before
来修复此问题,以确保首先加载它。
The problem is simply that
underscore.js
is being loaded afterbackbone.js
, when it's a prereq that has to be loaded before. (Notice in the Backbone.js source that it setsvar _ = root._
immediately, so even if a global_
is declared later, it's not visible from Backbone's scope.) Sprockets loads the JS files in your assets directory in alphabetical order by default.You can fix this using Sprockets: Put
before
to ensure that it's loaded first.