加载外部下划线模板
Load external underscore template
我在主干应用程序中加载外部下划线模板时遇到了麻烦。内联模板(注释掉了)工作得很好,但是外部模板不行。
$.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'
我猜它与我的应用程序在一个函数内,但我不确定如何解决这个问题。
在你调用$('#tpl_observation_summary').html()
的那一刻,DOM似乎还没有准备好,你得到null
作为_.template
的第一个参数,从而导致下划线不能处理一个不是字符串的字符串。
则不能定义bootstrap
方法,因此不能作为$.app
的函数调用。
模板初始化可以包含在Backbone视图的第一个initialize
调用中。
相关文章:
- es6 相当于下划线查找位置
- 用空格替换下划线PHP
- 如何使用下划线js转换这些数据
- 图像可以从源<img src=""/>.TEXT可以在没有javascript的情况下从外部
- 带嵌套json的下划线js查找
- 如何逃离<>在javascript下划线模板中
- TinyMCE从MSWord粘贴的文本在所有文本下加下划线
- 使用lodash下划线基于键拆分jsonarray
- 在控制台中显示下划线变量
- 如何从另一个带下划线的数组中筛选带元素的数组
- 筛选下划线中的对象
- 在键值对中对求和值进行下划线
- 使用下划线将键和值的两个数组合并到一个对象中
- 如何将文件名中的斜线替换为下划线
- 如何在结果中添加下划线但不添加虚假值的情况下进行映射
- 下划线用句点扩展.旋转引号被删除,如何用引号获得它
- 外部模板下划线
- 下划线和猫鼬:使用文档作为外部函数的返回值
- 加载外部下划线模板
- 使用下划线外部模板在背部骨骼中设置模型内容