0

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

    {{#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的上下文中是否有更多惯用的方法来做到这一点?

4

1 回答 1

1

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

{{#each notifications}}
{{/each}}
{{#unless notifications.length}}
{{/unless}}
于 2015-02-16T16:21:41.927 回答