【问题标题】:Change Marionette ItemView template dynamically动态更改 Marionette ItemView 模板
【发布时间】:2014-06-15 09:52:52
【问题描述】:

我在Marionette 中有这个ItemView

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>'),
});

我想知道是否可以动态创建模板? 因为有一段时间我想(基于检查某事的方法)删除small2 标签。

【问题讨论】:

    标签: javascript html backbone.js marionette


    【解决方案1】:

    Marionette has a function 调用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 */);
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      相关资源
      最近更新 更多