骨干& jQuery Tmpl - 将样式应用于模板项

时间:2011-08-17 10:42:19

标签: backbone.js jquery-templates

我的骨干视图如下所示,并使用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>

1 个答案:

答案 0 :(得分: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/