【发布时间】:2013-04-16 04:34:55
【问题描述】:
我在我的主干应用程序中加载外部下划线模板时遇到问题。内联模板(已注释掉)可以正常工作,但外部不能。
$.app.ObservationSummaryView = Backbone.View.extend({
tagName: 'div',
className: 'observation_summary_view',
// template: _.template('ID: <%= id %><p>Comments:<%= comments.length %>'),
template: _.template($('#tpl_observation_summary').html()),
initialize: function(){
_.bindAll(this, 'render', 'on_click');
this.model.bind('change', this.render);
},
render: function(){
return $(this.el).html(this.template(this.model.toJSON()));
},
on_click: function(e){
$.app.app.navigate('observation/' + this.model.get('id') + '/', {trigger: true});
}
});
HTML/模板:
<script type="text/template" id="tpl_observation_summary">
<%= id %>
</script>
<script type="text/javascript">
$(document).ready(function() {
(function($) {
$.app.bootstrap();
})(jQuery);
});
</script>
错误:
Uncaught TypeError: Cannot call method 'replace' of undefined underscore.js:1131
Uncaught TypeError: Object #<Object> has no method 'bootstrap'
我猜这与我的应用在函数中有关,但我不确定如何解决。
【问题讨论】:
-
模板编译时 DOM 准备好了吗?我认为它没有考虑你如何调用 main 方法。
-
已添加,结果相同 :(.
-
我的意思是 ObservationSummaryView 中的编译:$(...).html() 调用得太早,可能会失败
-
我会被诅咒的......谢谢。我想这就是 20 小时的编码马拉松将产生的结果。将其发布为答案,以便我给你信用:=)。
标签: javascript backbone.js underscore.js