Jquery mobile 带Backbone.js错误的下划线.js模板
下面这行完全失败了Jquery mobile 带Backbone.js错误的下划线.js模板,jquery-mobile,backbone.js,underscore.js,Jquery Mobile,Backbone.js,Underscore.js,下面这行完全失败了 template: _.template($('#test').html()), 尝试按照中的一个简单示例使用jQuery mobile和Backbone.js。我在web检查器中遇到的错误是:TypeError:“null”不是一个对象(计算'str.replace'),它位于underline.js的第913行。以这种方式使用is u0.template: template: _.template("<h1>To Do</h1>"), main
template: _.template($('#test').html()),
尝试按照中的一个简单示例使用jQuery mobile和Backbone.js。我在web检查器中遇到的错误是:TypeError:“null”不是一个对象(计算'str.replace'),它位于underline.js的第913行。以这种方式使用is u0.template:
template: _.template("<h1>To Do</h1>"),
main.html
<script type = 'text/template' id = 'test'> <h1>To Do</h1> </script>
要做的事
解析视图时,DOM未就绪:
TodoBb.Views.ComCentersTodo = Backbone.View.extend({
template: _.template($('#test').html()),
//...
});
所以$('#test').html()
是null
,您实际上是这样做的:
TodoBb.Views.ComCentersTodo = Backbone.View.extend({
template: _.template(null),
//...
});
将模板转换为JavaScript函数时使用的内部代码
您有几个选择:
TodoBd.Views.ComCentersTodo
定义放入$(文档).ready()
处理程序中:
$(function() {
TodoBb.Views.ComCentersTodo = Backbone.View.extend({
template: _.template($('#test').html()),
//...
});
});
TodoBb.Views.ComCentersTodo = Backbone.View.extend({
//... no template in here
render: function() {
var html = _.template($('#test').html(), this.model.toJSON());
this.$el.html(html);
return this;
},
//...
});
作为2的一种变体,您可以将编译好的模板函数缓存在某个地方,并且在第一次使用它时只调用
.template($('.\test').html())
。那么问题出在哪里呢?jQuery mobile样式是否未出现?为此,您需要触发create事件,如$(“…”)。trigger(“create”)
,尽管现在只查看文档,我找不到create事件
TodoBb.Views.ComCentersTodo = Backbone.View.extend({
//... no template in here
render: function() {
var html = _.template($('#test').html(), this.model.toJSON());
this.$el.html(html);
return this;
},
//...
});