我想在模板中显示我的模型,但我遇到了一个问题:
Uncaught ReferenceError: title is not defined
我的代码是:
var NewsView2 = Backbone.View.extend({
NewsView: _.template(NewsView),
initialize: function () {
console.log(this);
this.$el.html(this.NewsView());
this.render();
},
render: function () {
this.$el.html(this.NewsView());
var html = this.NewsView(this.model.toJSON());
this.$el.append(html);
return this;
}
});
初始化:
var news2 = new News({
author: "costam",
});
var widok2 = new NewsView2({model: news2});
和我的模板中的代码:
<html>
<head>
</head>
<body>
<%= title %>
</body>
有人可以帮我吗?我不知道这样做。
答案 0 :(得分:0)
您的模型没有标题属性。 UnderScore模板正在抱怨,因为它试图在新闻模型中查找不存在的title属性。
你的骨干视图:
var NewsView2 = Backbone.View.extend({
initialize: function () {
this.render();
},
render: function () {
var template = _.template($("#yourTemplate").html())
var html = template(this.model.toJSON());
this.$el.append(html);
return this;
}
});
你的模特:
var news2 = new News({ author: "costam", title : "Your title" });
模板:
<html>
<head>
</head>
<body>
<script type="text/template" id="yourTemplate">
<%= author %> // --> this should match/exist in your model
<%= title %> // --> this should match/exist in your model
</script>
</body>
注意:有不同的方法来设置主干模板,如this。