骨干入门

时间:2012-12-05 18:34:10

标签: backbone.js

业余这个问题可能是但也可以用一只手。刚从包装中取出骨干并启动了hello world示例,但似乎无法启动并运行。有人可以告诉我为什么我没有看到这个结果?

(function($){

var ListView = Backbone.View.extend({
    el: $(body), //attaches this.el to an existing element

    initialize: function(){
        _.bindAll(this, 'render'); //fixes loss of context for this within elements
        this.render(); //not all views are self-rendering. This is one.
    },

    render: function(){
        $(this.el).html("<ul><li>Hello World!</li></ul>");
    }
});
var listVew = new ListView();


})(jQuery);

HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Hello Backbone </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://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>

<script src="backbone.js" type="text/javascript"></script>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

  • 我在jsfiddle中尝试了您的代码并收到此错误:Uncaught ReferenceError: body is not defined
    • 使用字符串'body'作为视图的el选择器

这有效:http://jsfiddle.net/PeGW6/

另请注意,您使用的是非常旧的骨干版本。升级到最新版本。