我有一个用户模型,每个用户有一个艺术家,每个艺术家有几张专辑。我试图呈现一个视图来显示用户配置文件。当我尝试渲染视图时,我得到以下错误:
Uncaught ReferenceError: albums is not defined
(anonymous function) b.template.c underscore-min.js:30
Backbone.View.extend.render profile.js:13
Backbone.Router.extend.profile router.js:62
...
它似乎是我没有传递一个专辑对象到模板,但我没有使用任何专辑变量在该模板,也没有在视图。下面是两者的代码:
视图:
headerT = require('text!templates/user/profile_header.html');
profileT = require('text!templates/user/profile.html');
var profilesView = Backbone.View.extend({
el: $('#main-container'),
initialize: function(){
this.template = _.template(profileT);
this.artist = this.model.get('artist');
},
render: function(){
$(this.el).html(this.template({
current_user: app.current_user,
user: this.model.toJSON(),
artist: this.artist.toJSON(),
}));
return this;
},
});
模板:
<div class="row">
<div class="grid_3">
<img src="<%=user.pict%>" class="frame" alt="">
<span class="title">Username – <strong><%=user.username%></strong></span>
<%if(current_user!=undefined && (current_user.get('is_admin') == true || current_user.get('id') == user.id)){%>
<span class="title"><span class="icon user"></span> <a href="#/editProfile/">Edit Profile</a></span>
<%}%>
<%if(artist.active==true){%>
<div><a href="#/artistProfile/">Go to Artist Profile</a></div>
<%}%>
<div class="separator"></div>
</div>
<div class="clear"></div>
</div>
我改变了保存模板的变量的名称,它工作了。我有另一个具有相同名称的变量,在另一个文件中保存一个不同的模板(其中包含相册),它正在加载那个。我以为作用域是不同的,因为它在另一个文件中,但它没有。