想从骨干视图中的html中加载外部模板 并显示模型人的值,但在控制台中得到错误说模板没有定义我的代码是对的那么什么问题
<script type="text/template" id="personTemplate" >
<strong><%= name %></strong>
<strong><%= age %></strong>
<strong><%= occupation %></strong>
</script>
var Person = Backbone.Model.extend({
initialize: function () {
this.on("invalid", function (model, error) {
alert(error);
});
},
defaults: {
name: 'Guest User',
age: 23,
occupation: 'worker'
}
});
var PersonView = Backbone.View.extend({
template: "#personTemplate",
initialize: function () {
alert("view success");
this.render();
},
render: function () {
template = _.template($(template).html())
this.$el.html(template, this.model.toJSON());
}
});
$(document).ready(function () {
var person = new Person
var personView = new PersonView( { model:person });
personView.el;
$(document.body).html(personView.el);
});
答案 0 :(得分:2)
尝试
template = _.template($(this.template).html());
在render
函数中。