本文介绍了未捕获类型错误:无法调用“扩展”的未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个CoffeeScript的类来扩展Backbone.Model。我建立了一个全新的轨道3.1应用程序,创造了'石'的支架与3个属性,并修补Todos.coffee例子的一个片段到stones.js.coffee。我在app /资产/ JavaScript的文件夹都Backbone.js的和underscore.js。当我运行这个浏览器Java控制台下,我得到上面在控制台日志消息。任何想法?

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?

实际code如下:

$ ->

  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生成的内容。我复制从托多斯GitHub库的Backbone.js的和underscore.js,

推荐答案

这个问题很简单,就是 underscore.js 骨干后加载。 JS ,当它是一个必须之前加载的preREQ。 (在它设置 VAR _ =根。 _ 立即,所以即使一个全球性的 _ 后声明,这是不可见的,从骨干的范围)。链轮加载在您的资产目录中的JS文件在默认情况下,按字母顺序排列。

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

//= require_tree .

要确保它的第一次加载。

to ensure that it's loaded first.

这篇关于未捕获类型错误:无法调用“扩展”的未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:04