Javascript 未捕获的类型错误:未定义不是函数 rails3/backbone/js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5797437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: undefined is not a function rails3/backbone/js
提问by blueblank
I just started delving into javascript to make project more responsive and I am working through a backbone.js example.
我刚刚开始深入研究 javascript 以使项目更具响应性,我正在研究一个backbone.js 示例。
I've replicated http://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/in a new Rails 3 project.
我在一个新的 Rails 3 项目中复制了http://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/。
I run the project and go to the home page .... and there is a link there to /# and nothing else. Looking into the js console I get errors on two scripts: application.js and backbone.js
我运行该项目并转到主页....那里有一个指向 /# 的链接,没有别的。查看 js 控制台,我在两个脚本上遇到错误:application.js 和backbone.js
This (backbone.js)
这个(backbone.js)
backbone-min-0-3-3.js:8Uncaught TypeError: Cannot call method 'extend' of undefined
and this (application.js):
和这个(application.js):
var App = {
Views: {},
Controllers: {},
Collections: {},
init: function() {
new App.Controllers.Fffforms();
**error message ---> application.js:9Uncaught TypeError: undefined is not a function**
Backbone.history.start();
}
};
Being new to js, this doesn't exactly make sense and nothing I've looked up has been all that helpful in the short term.
作为 js 的新手,这并不完全合理,而且我所查找的任何内容在短期内都没有那么有用。
Can anyone tell me what exactly these errors would indicate and how I can go about following up? Everything checks out comparing the resources in https://github.com/jamesyu/CloudEdit, but my replication from a new rails 3 project (not a clone of that repo) doesn't exactly work.
谁能告诉我这些错误究竟意味着什么,以及我该如何跟进?一切检查比较https://github.com/jamesyu/CloudEdit 中的资源 ,但我从一个新的 rails 3 项目(不是该 repo 的克隆)复制并不完全有效。
Any suggestions appreciated, bearing in mind I've just embarked on learning some javascript.
任何建议表示赞赏,记住我刚刚开始学习一些 javascript。
EDIT:
编辑:
By suggestion, I went and actually added in the Jammit gem and configured it to serve all the js scripts, which the default Rails all was not. Now all the scripts are going to the browser (controller included). Unfortunately, this doesn't solve the original issue, only giving more errors on load, flowing from the App init which is this in the chrome js console:
根据建议,我实际上添加了 Jammit gem 并将其配置为提供所有 js 脚本,而默认的 Rails 并非如此。现在所有脚本都将进入浏览器(包括控制器)。不幸的是,这并不能解决最初的问题,只会在加载时出现更多错误,这些错误来自 chrome js 控制台中的 App init:
Uncaught TypeError: undefined is not a function
App.initapplication.js:9
(anonymous function):3000/#new:32
d.extend._Deferred.f.resolveWithjquery.min.js:16
d.d.extend.readyjquery.min.js:16
d.c.addEventListener.yjquery.min.js:16
Given I'm just copying right now, there must be some small overlooked detail beyond me atm that is preventing App from initiating properly.
鉴于我现在只是在复制,一定有一些被我忽视的小细节导致应用程序无法正常启动。
采纳答案by Andrew Hare
It sounds like you are not including the file that holds the declaration of App.Controllers.Fffforms
. Make sure that you are including that file somewhere in you code prior to where you include application.js.
听起来您不包括包含App.Controllers.Fffforms
. 确保在包含 application.js 之前在代码中的某个位置包含该文件。
回答by bstst
I presume there is a kind of bundling mechanism there in your app. Make sure all files have correct usage of semicolons (;) in all bundled files.
我认为您的应用程序中存在一种捆绑机制。确保所有文件在所有捆绑文件中都正确使用了分号 (;)。
回答by Tom Harrison
My answer is similar to @ream88's, but Rails 3.1+ Asset Pipeline feature takes care of minification, bundling and so on, so I prefer to have the un-minified versions available for debugging, etc.
我的回答与@ream88 类似,但 Rails 3.1+ Asset Pipeline 功能负责缩小、捆绑等,所以我更喜欢将未缩小的版本用于调试等。
So download the commented/full version of backbone.jsand underscore.jsand save them in app/assets/javascripts
(you could also save them in vendor/assets/javascripts
).
因此,下载backbone.js和underscore.js的注释/完整版本并将它们保存在app/assets/javascripts
(您也可以将它们保存在 中vendor/assets/javascripts
)。
The difference is that you should update the manifest file (app/assets/javascripts/application.js
) to add the require directives, like so
不同之处在于您应该更新清单文件 ( app/assets/javascripts/application.js
) 以添加 require 指令,如下所示
//= require jquery
//= require jquery_ujs
//= require underscore
//= require backbone
//= require_tree .
Because backbone depends on underscore, this will cause them to get loaded in the right order, thus avoiding the error.
因为主干依赖于下划线,这将导致它们以正确的顺序加载,从而避免错误。
回答by jwjwj
This just bit me so I thought I'd share my find: make sure your file's line endings match your server's file system.
这只是咬我,所以我想我会分享我的发现:确保您的文件的行尾与您的服务器的文件系统相匹配。
回答by Mario Uher
Ran into the same problem, and then I figured out that I haven't included underscore.js
anywhere. So I wrote a simple backbone.js
file:
遇到了同样的问题,然后我发现我没有包含underscore.js
任何地方。所以我写了一个简单的backbone.js
文件:
/*
*= require backbone/underscore-min.js
*= require backbone/backbone-min.js
*/
and stored it under vendor/assets/javascripts
along with the Backbone and Underscore files:
并将其vendor/assets/javascripts
与 Backbone 和 Underscore 文件一起存储在下面:
vendor/assets/javascripts/
├── backbone
│?? ├── backbone-min.js
│?? └── underscore-min.js
└── backbone.js
My application.js.coffee
looks something like this one now:
我application.js.coffee
现在看起来像这样:
#= require backbone
#= require query