之所以不断打印项目名称是因为,在模板中,你有...
<a href="#"><%= projectName === projectName ? projectName : taskStatus%></a>
projectName
总是等于projectName
。在这种情况下,您要做的是,在您的 Backbone.View 中,当您序列化要由模板使用的模型时,您可以执行此操作...
Backbone.View.extend({
events: {
// An example on how to change the display
'click button.change-display': 'onChangeDisplayClicked'
},
template: _.template(...),
// Controls whether project name should be shown or not.
showProjectName: true,
onChangeDisplayClicked: function() {
// Flip the switch
this.showProjectName = !this.showProjectName;
// Re-render the View
this.render();
},
serialize: function() {
// Grab the data from model
var data = this.model.toJSON();
// Pass this data to the template to control what to be displayed.
data.showProjectName = this.showProjectName;
return data;
},
render: function() {
this.$el.html(this.template(this.serialize()));
}
});
...在您的模板中,您将...
<script id="listTemplate" type="text/template">
<a href="#"><%= showProjectName ? projectName : taskStatu s%></a>
</script>