2017-07-13 9 views
2

これは私が持っているものです。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

新しいヘルパーを追加します。 islast:function(i){return news.find()。count()-1 === i;} – iiro

+0

@alvespedroこの投稿は役に立ちましたか?https://stackoverflow.com/questions/21815713/in-meteor-is-スペースバーの配列インデックスへのアクセス方法 –

答えて

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

は、パラメータとして@indexを受け入れること、それlastを呼び出し、助けを{{#unless last @index}},{{/unless}}

関連する問題