Meteor + Blaze - 如何仅在循环的最后一个元素中使用条件?

时间:2017-07-13 15:53:02

标签: meteor meteor-blaze

这就是我所拥有的:

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}}但它不起作用。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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>