我遇到了Backbone.js视图不渲染的问题。我的代码非常简单,看起来像这样:
TableView = Backbone.View.extend({
initialize : function() {
this.render();
},
render : function() {
var template = _.template($("#table_template").html(), {});
alert(this.el);
this.el.html('Go');
//this.el.html(template);
},
events: {
},
});
以下是实例化对象并设置el的代码:
<script type="text/javascript">
$(document).ready(function() {
var t = $("#table_1");
//This works!!!
t.html('Test');
//Passing the element as the el, never works
var table = new TableView({el : t});
});
</script>
但是控制台总是显示以下错误:未捕获的TypeError: Object #<HTMLDivElement>没有html方法。
我在这里做错了什么?我正在使用Jquery.1.7.2、backbone 0.9.2、underscore 1.3.3和json2。
el
是一个元素而不是jQuery对象,但是在this.el.html(template);
中却将其用作jQuery对象。文章的评论也指出了这一点,但他还没有更新它... - Esailija