【问题标题】:Meteor + Blaze - How can use a condition only in the last element of my loop?Meteor + Blaze - 如何仅在循环的最后一个元素中使用条件?
【发布时间】:2017-07-17 23:54:59
【问题描述】:

这就是我所拥有的:

Template.publicnewsjson.helpers({ 

news:function(){      

   return news.find({}, { sort: {date:-1} } );

   },

   newscount:function(){

    return news.find().count();
   }
});


  <template name="publicnewsjson">
   <pre>
     {{#each news}}
        {
              Title:{{title}}
              Date:{{friendlydate this.date}}
              Abstract:{{abstract}}
              HeadlineImagePath:{{headlineimagepath}}
              URL:{{url}}
              Source:{{source}}
        }, <------- This is the comma that I want to remove in the last repetition
     {{/each}}
   </pre>
  </template>

如何在最后一次重复中声明以获取逗号? 我正在尝试类似的东西?:

{{#if newscount @index}} 但它不起作用。

【问题讨论】:

标签: meteor meteor-blaze


【解决方案1】:

您可以创建一个辅助函数,我们称之为last,它接受@index 作为参数并使用{{#unless last @index}},{{/unless}}

【讨论】:

    【解决方案2】:

    感谢您的帮助,我能够解决我的问题。 那是我的新代码:

    Template.publicnewsjson.helpers({
    
    
       news:function(){  
    
       TAPi18n.subscribe('publicnewslistall', null);  
    
       return news.find({}, { sort: {date:-1} } );
    },
    
    islast:function(position){
    
     TAPi18n.subscribe('publicnewslistall', null);
     var size = news.find().count();
    
        if( size === position+1){
          console.log("ultimo");
          return true;
        }
        return false;
       }
    });
    
    <template name="publicnewsjson">  
    
    <pre>[{{#each news}}{{#if islast @index}}{
                                          "Title":"{{title}}",
                                          "Date":"{{friendlydate this.date}}",
                                          "Abstract":"{{abstract}}",
                                          "HeadlineImagePath":"{{headlineimagepath}}",
                                          "URL":"{{url}}",
                                          "Source":"{{source}}"
                                    }{{else}}
    
                                    {
                                          "Title":"{{title}}",
                                          "Date":"{{friendlydate this.date}}",
                                          "Abstract":"{{abstract}}",
                                          "HeadlineImagePath":"{{headlineimagepath}}",
                                          "URL":"{{url}}",
                                          "Source":"{{source}}"
                                    },{{/if}}{{/each}}]</pre>         
    </template>
    

    【讨论】:

      猜你喜欢
      • 2014-11-06
      • 2016-02-22
      • 2015-04-22
      • 1970-01-01
      • 2013-12-15
      • 2018-05-04
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      相关资源
      最近更新 更多