动态更改Marionette ItemView模板

MTA*_*MTA 3 html javascript backbone.js marionette

我有这样ItemViewMarionette:

App.View.MovieListItem = Marionette.ItemView.extend({
tagName: 'li',
className: 'movie',
model: App.Model.Movie,
id: function() {
    return 'movie-'+this.model.get('imdb')
},

initialize: function () {
    this.render();
},

template: _.template('<a href="javascript:;">'+
        '<i class="fa fa-eye fa-3"></i>'+
        '<span class="cover"></span>'+
        '<strong><%= title %></strong>'+
        '<small><%- year %></small>'+
        '<small2>Cached</small2>'+
    '</a>'),
});
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以动态创建模板?因为有些时候我想要(基于检查某些东西的方法)来删除small2标签.

Cub*_*Eye 10

Marionette有一个名为的函数getTemplate,可用于返回动态模板.

例:

App.View.MovieListItem = Marionette.ItemView.extend({
    tagName: 'li',
    className: 'movie',
    model: App.Model.Movie,
    id: function() {
        return 'movie-'+this.model.get('imdb')
    },

    initialize: function () {
        this.render();
    },

    getTemplate: function(){
        if(condition){
            return _.template(/* HTML STRING */);
        }else{
            return _.template(/* ANOTHER HTML STRING */);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)