javascript 未捕获的 ReferenceError:未定义 Backbone
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21939090/
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 ReferenceError: Backbone is not defined
提问by D4V1D
I am completely new to Backbone JS. I thought I could solve this minor problem by myself but I can't figure why I am still getting this error:
我对 Backbone JS 完全陌生。我以为我可以自己解决这个小问题,但我不明白为什么我仍然收到此错误:
Uncaught ReferenceError: Backbone is not defined
未捕获的 ReferenceError:未定义 Backbone
when trying to extend a Backbone.Model. backbone.js
is called before the script that uses it so I don't get it.
尝试扩展 Backbone.Model 时。backbone.js
在使用它的脚本之前调用,所以我不明白。
index.html
索引.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/backbone.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<title></title>
</head>
<body>
</body>
</html>
My external file main.js
我的外部文件 main.js
(function($) {
window.Doc = Backbone.Model.extend({
defaults : {
id : '???',
title : 'Le titre de mon modèle',
text : 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer varius ipsum nec porta dignissim. Donec a elementum magna. Donec sagittis magna eu nulla ullamcorper dictum. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nullam volutpat felis vehicula, congue mi at, lobortis dolor.',
keywords : 'lorem ipsum dolor sit amet'
},
initialize : function Doc() {
console.log('Doc Constructor');
}
});
})(jQuery);
I also get the following error coming from backbone.js:219
我还收到来自backbone.js:219的以下错误
Uncaught TypeError: Cannot call method 'each' of undefined
未捕获的类型错误:无法调用未定义的方法“每个”
回答by LudovicL
You forgot to import underscore.js
.
It is a Backbone requirement.
你忘记导入了underscore.js
。这是一个 Backbone 要求。
回答by Anvita Shukla
I faced the same issue while running jasmine testcases using SpecRunner.html, so when specifying backbone.js in your dependencies in SpecRunner.html, add jquery and underscore.js first, because backbone depends on them.
我在使用 SpecRunner.html 运行 jasmine 测试用例时遇到了同样的问题,所以在 SpecRunner.html 的依赖项中指定backbone.js 时,首先添加 jquery 和 underscore.js,因为主干依赖于它们。