【问题标题】:Backbone & jQuery Tmpl - applying style to template itemBackbone & jQuery Tmpl - 将样式应用于模板项
【发布时间】:2011-10-28 19:41:30
【问题描述】:

我的主干视图如下所示,并使用 jQuery tmpl 库呈现。我想将样式应用于一个/所有/任何数据项 活跃==1。关于如何做到这一点的任何想法?

   // backbone view 
   window.CaseView = Backbone.View.extend({

    el: $("#main"),

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

    iTemplate: $("#tmplCase").template(),

    render: function() {
        var that = this;
        that.el.fadeOut('fast', function() {
            $.tmpl(that.iTemplate, that.model.toJSON()).appendTo(that.el);
            that.el.fadeIn('fast');
        });

        return this;
    }
});

// html file
<div id="main"></div>

<script id="tmplCase" type="text/x-jquery-tmpl">
  <div class="caseInActive">
    <span class="title">${title}</span>
    <span class="current_status">${active}</span>
  </div>
</script>

【问题讨论】:

    标签: backbone.js jquery-templates


    【解决方案1】:

    您可以在模板中添加 if 语句:

    // html file
    
    <script id="tmplCase" type="text/x-jquery-tmpl">
    
    
      <div {{if active == 1}}class="caseInActive"{{/if}}>
    
    
        <span class="title">${title}</span>
        <span class="current_status">${active}</span>
      </div>
    </script>
    

    http://api.jquery.com/template-tag-if/

    【讨论】:

      猜你喜欢
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2011-08-18
      • 2012-09-29
      相关资源
      最近更新 更多