未捕获的类型错误:无法调用方法“扩展”未定义的

发布于 2024-12-25 10:58:22 字数 1058 浏览 1 评论 0原文

我正在尝试获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天暗了我发光 2025-01-01 10:58:22

问题很简单,underscore.jsbackbone.js 之后加载,而它是必须在之前加载的先决条件。 (请注意 Backbone.js 源 它设置了 var _ = root._ 立即生效,因此即使稍后声明全局 _,它在 Backbone 的作用域中也是不可见的。)Sprockets 默认按字母顺序加载资产目录中的 JS 文件。

您可以使用 Sprockets: Put

//= require underscore.js

before

//= require_tree .

来修复此问题,以确保首先加载它。

The problem is simply that underscore.js is being loaded after backbone.js, when it's a prereq that has to be loaded before. (Notice in the Backbone.js source that it sets var _ = 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

//= require underscore.js

before

//= require_tree .

to ensure that it's loaded first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文