Google Chrome开发者工具控制台会抛出此错误:
uncaught reference error: backbone is not defined
当这个html文件包含javascript with library backbone.js:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Backbone APP</title>
</head>
<body>
<script>
Whisky = Backbone.Model.extend();
firstWhisky = new Whisky({
name : 'Blenders Pride'
});
Whiskies = Backbone.Collection.extend({
Model:Whisky ,
url:"#"
});
first_view = Backbone.View.extend({
el : 'body',
initialize : function() {
this.$el.empty();
this.render();
} ,
render : function() {
this.$el.append("<h1>The Whisky APP</h1>");
this.list_view = new List_view();
this.$el.append(this.list_view.render().el);
return this ;
}
});
List_view = Backbone.View.extend({
tagName : 'ul' ,
render : function() {
this.$el.empty();
this.$el.append("<li>Royal stag</li>");
this.$el.append("<li>Signature </li> ");
return this ;
}
});
index_view = new first_view();
</script>
<script src="LIB/json2.js"></script>
<script src="LIB/underscore-min.js"></script>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="http://backbonejs.org/backbone.js"></script>
</body>
</html>
出现此错误的原因是什么?
答案 0 :(得分:5)
在声明之前你不能使用Backbone。
将<script>
标记放在您自己的javascript代码之前。
您必须重新整理您的javascript部分:
<script src="LIB/json2.js"></script>
<script src="LIB/underscore-min.js"></script>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="http://backbonejs.org/backbone.js"></script>
<script>
Whisky = Backbone.Model.extend();
firstWhisky = new Whisky({
name : 'Blenders Pride'
});
Whiskies = Backbone.Collection.extend({
Model:Whisky ,
url:"#"
});
first_view = Backbone.View.extend({
el : 'body',
initialize : function() {
this.$el.empty();
this.render();
} ,
render : function() {
this.$el.append("<h1>The Whisky APP</h1>");
this.list_view = new List_view();
this.$el.append(this.list_view.render().el);
return this ;
}
});
List_view = Backbone.View.extend({
tagName : 'ul' ,
render : function() {
this.$el.empty();
this.$el.append("<li>Royal stag</li>");
this.$el.append("<li>Signature </li> ");
return this ;
}
});
index_view = new first_view();
</script>
答案 1 :(得分:2)
这可能是相关的。 我使用以下链接从http://cdnjs.com托管库向我的index.html加载jQuery,Underscore和Backbone:
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
因此我可以在Chrome中访问$,_和Backbone,但如果我尝试在Firefox中打开该页面,我会得到:未捕获的ReferenceError:Backbone未定义.. 但是,当使用Eric Leschinski提供的链接时:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>
一切正常。
答案 2 :(得分:1)
<html>
<body>
<script type="text/javascript">
var AppView = Backbone.View.extend({
});
</script>
<div>mydiv</div>
</body>
</html>
您收到错误消息:
ReferenceError: Backbone is not defined
var AppView = Backbone.View.extend({
您尚未包含必要的主干库。
此示例已定义主干:
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>
<div>Loading...</div>
<script type="text/javascript">
alert("starting backbone");
var AppView = Backbone.View.extend({
el: 'div',
initialize: function(){
alert("initializing backbone");
this.render();
},
render: function(){
alert("applying text to div attribute");
this.$el.html("Hello World");
}
});
var appView = new AppView();
alert("done");
</script>
</body>
</html>
要在jsfiddle中运行此示例,请参阅此处:http://jsfiddle.net/ZfnbK/
它应该显示4个弹出框,说明它通过代码的路径。调用render函数时,backbone使用jQuery将div标签的内容替换为:“Hello World”。