2011-08-17 59 views
0

我的骨幹視圖如下所示,並使用jQuery tmpl庫進行呈現。我想將樣式應用於其中一個/所有/任何數據項,其中 活動== 1。任何想法如何做到這一點?Backbone&jQuery Tmpl - 將樣式應用於模板項目

// 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

可以添加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/

+0

感謝,這種做法並沒有涌現在腦海中的。 – Joe 2011-08-17 14:13:21