当我尝试导入如下所示的模板时,我似乎遇到了backbone.js / underscore.js 的砖墙:
<script type="text/template" id="overview_template">
<div>
Sample text
</div>
</script>
错误内容如下:
Uncaught TypeError: Object #<HTMLDivElement> has no method 'html' navigation.js:356
Backbone.View.extend.render navigation.js:356
Backbone.View.extend.initialize navigation.js:351
g.View backbone-min.js:33 d backbone-min.js:38
(anonymous function) navigation.js:379
f.Callbacks.n jquery-1.7.1.min.js:2
f.Callbacks.o.fireWith jquery-1.7.1.min.js:2
e.extend.ready jquery-1.7.1.min.js:2 c.addEventListener.B
触发错误的代码this.el.html(template);
如下:
var OverviewView = Backbone.View.extend({
el: $('#overview_container'),
initialize: function() {
this.render();
},
render: function() {
var template = _.template( $("#overview_template").html(), {} );
this.el.html(template);
},
defaults: {
tip_of_the_day: 'open',
news: 'open',
recent_presentations: 'open'
},
events: {
"click .overview_subsection_header": "toggleSubsection"
},
toggleSubsection: function (event) {
$(this).parent().find('.overview_subsection_content').toggle();
}
});
var overview_view = new OverviewView();
我不确定是什么原因造成的,但它一直让我发疯。