我有一个html模板,其中包含骨干代码。我是骨干的新手。每当我尝试运行代码时,它会显示未捕获的错误undefined不是函数错误。请帮助。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello-backbonejs</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>
<script type="text/javascript">
(function($){
//ListView class: Our main app view.
var ListView = Backbone.View.extend({
el: $('body'), // attaches `this.el` to an existing element.
//initialize(): Automatically called upon instantiation. Where you make all types of bindings, excluding UI events, such as clicks, etc.
initialize: function(){
_.bindAll(this, 'render'); // fixes loss of context for 'this' within methods
this.render(); // not all views are self-rendering. This one is.
},
//render(): Function in charge of rendering the entire view in this.el. Needs to be manually called by the user.
render: function(){
$(this.el).append("<ul> <li>hello world</li> </ul>");
}
});
//listView instance: Instantiate main app view.
var listView = new ListView();
})(jQuery);
</script>
</body>
</html>
答案 0 :(得分:0)
问题是由旧版本的 underscore.js 引起的。将其更改为较新版本后,错误消失,您的代码也能正常工作。
演示中使用的下划线版本:
//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js
P.S。如果你在jsFiddle的左手风琴菜单中查看外部资源,你可以看到导入的文件(在HEAD中)。