我有一个html模板,其中包括骨干代码。我是backbone.whenever的新手,我试着运行代码,它显示了一个未定义的未定义的错误不是函数error.Please帮助。
<!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>发布于 2014-06-03 06:21:35
这个问题是由旧版本的underscore.js引起的。将其更改为较新版本后,错误就消失了,您的代码也可以工作了。
演示中使用的下划线版本:
//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js如果您查看jsFiddle上左侧手风琴菜单中的外部参考资料,您可以看到导入的文件(在头中)。
https://stackoverflow.com/questions/24008152
复制相似问题