在canjs中,基于任何模型实例的存在,有条件地显示小胡子模板的部分的最惯用的方法是什么?

时间:2015-02-13 18:33:02

标签: javascript mustache canjs canjs-model canjs-view

我在胡子样式模板中迭代每个模型实例,并且想要一个干净的方式来显示没有可用实例的消息(例如,如果有人删除/销毁它们)

    {{#notifications}}
      <div class='tertiary'>
        <li>Sent {{& displayFrequency frequency}} <br> to <br> <span class='strong'>{{recipients}}</span></li>
        <div id='action-buttons' {{data "notification"}}>    
          <span class='notification-option'><button class='edit'>edit details</button></span>
          <span  class='notification-option'><button class='delete'>delete</button></span>
          <span class='notification-option'>
            <input class="is-active" type="checkbox" {{#if active}}checked{{/if}}>
          </span>
        </div>      
      </div>      
      <br><br>    
    {{/notifications}}

首先想到的是:

  {{#if notifications.length}}
    {{#notifications}}
      <div class='tertiary'>
        <li>Sent {{& displayFrequency frequency}} <br> to <br> <span class='strong'>{{recipients}}</span></li>
        <div id='action-buttons' {{data "notification"}}>    
          <span class='notification-option'><button class='edit'>edit details</button></span>
          <span  class='notification-option'><button class='delete'>delete</button></span>
          <span class='notification-option'>
            <input class="is-active" type="checkbox" {{#if active}}checked{{/if}}>
          </span>
        </div>      
      </div>      
      <br><br>    
    {{/notifications}}
  {{else}}
    <div>No Notifications</div>        
  {{/if}}

有更好的方法吗?在canjs的背景下有更多惯用的方法吗?

1 个答案:

答案 0 :(得分:1)

您也可以使用#unless标签来避免嵌套:

{{#each notifications}}
{{/each}}
{{#unless notifications.length}}
{{/unless}}