2015-01-10 89 views
0

我試圖實現與TelescopeJS類似的東西,但更容易。所以我只想知道是否有辦法檢查作者的ID。用於返回isAuthor函數的流星幫助函數

我想要做的是僅顯示文章作者的刪除按鈕。我的文章有這些字段的集合:

Articles.insert({description:description,name:name,hashtag:hashtag,src:url,author:Meteor.userId()}); 

我希望刪除按鈕僅顯示給帖子的作者。所以我需要一個輔助函數來返回布爾值。 該功能應該遵循:如果當前用戶ID等於作者的用戶ID,則返回true,否則返回false。現在這是一個相當簡單的功能,但我不知道如何在集合中調用作者字段。

在此先感謝!

JS代碼:

Template.pinterest.helpers({ 
    articles: function() { 
     var search = {}; 
     return Articles.find(search, { 
      limit: 20 
     }); 
    }, 
    adding_interest: function() { 
     return Session.get('adding_interest'); 
    }, 
    numlikes: function() { 
     return Likes.find({ 
      article: this._id 
     }).count(); 
    }, 
    likethis: function() { 
     var curUserlike = Likes.findOne({ 
      muser: Meteor.userId(), 
      article: this._id 
     }); 
     if (curUserlike) { 
      return "You Like This"; 
     } else { 
      return "Thumbs up!"; 
     } 
    }, 
    updated: function() { 
     return Session.get('updated'); 
    }, 
    isAuthor: function() { 
     if (this.author === Meteor.userId()) { //this.author is author in doc 
      console.log(this.author); 

     } 

    } 

}); 

Template.article.helpers({ 
    numlikes: function() { 
     return Likes.find({ 
      article: this._id 
     }).count(); 
    }, 


    userName: function() { 
     return Meteor.user().username || Meteor.user().profile.name || Meteor.userId() 
    }, 
    userimage: function() { 
     if (Meteor.user().services.facebook) { 
      return "http://graph.facebook.com/" + Meteor.user().services.facebook.id + "/picture/?type=large"; 
     } 
    }, 

    timestamp: function() { 
     return new Date(); 
    }, 
    likethis: function() { 
     var curlike = Likes.findOne({ 
      muser: Meteor.userId(), 
      article: this._id 
     }); 
     if (curlike) { 
      return "You Like This"; 
     } 
    } 
}); 


Template.pinterest.rendered = function() { 
    setTimeout(function() { 
     masonize(function() {}); 

    }, 1000) 
    $('.search-query input').focus(); 

} 

HTML模板

<template name="article"> 
<div class="item"> 
    <div class="ui special cards"> 
    <div class="card"> 
    <div class="dimmable image"> 
     <div class="ui dimmer"> 
     <div class="content"> 
      <div class="center"> 
    {{#if currentUser}} 
      <div class="ui inverted button"> 

       <a href="#" class="like"> 
       <i class="heart icon"></i> Like 
      </a> 
      </div> 
       {{/if}}  
      </div> 
     </div> 
     </div> 

    <img src="{{src}}"/> 

    <script>$('.special.cards .image').dimmer({ 
    on: 'hover' 
});</script> 

    </div> 
    <div class="content"> 
     <a class="header">{{name}}</a> 
     <div class="meta"> 
     <span class="date">{{timestamp}}</span><br><a href="#">{{hashtag}}</a> 
     </div> 
    </div> 
    <div class="extra content"> 
     <a> 

     {{#if currentUser}}  
     <p> 
      <a href="#" class="like"> 
      <i style="color:#564f8a;" class="heart icon"></i> 
      </a> 

      <a class="ui purple circular label"> 
       {{numlikes}} likes 
       </a>&nbsp;&nbsp; 
       <div class="ui red horizontal label">{{likethis}}</div><br> 
      **{{#if isAuthor}} 
       <a class="remove"><i style="color:#d95c5c;" class="remove icon">{{removeArticle}}</i></a> 

      {{/if}}** 

     </p><br> 
     {{/if}} 
     </a> 
     <div class="description"> 
     <p>{{description}}</p> 

    </div> 
    {{#if currentUser}} 
    <hr>  
    <div class="extra content"> 
    <div class="right floated author"> 
     <img class="ui avatar image" src="{{userimage}}"> {{author}} 
    </div> 
    </div> 
    {{/if}} 
    </div> 
    </div> 
</div> 

</div> 

</template> 

回答

0

的isAuthor字段添加到每篇文章與變革。喜歡這個。

// the articles helper 
articles: function(){ 
    var userId = Meteor.userId(); 
    return Articles.find({},{transform: function (doc){ 
    doc.isAuthor = doc.author === userId; 
    return doc; 
    }}); 
} 
+0

夥計,你是個天才! :D感謝一百萬! :D:D –

+0

我真的不能非常感謝你! :d –

0
{{#if isAuthor}} 
       <a class="remove"><i style="color:#d95c5c;" class="remove icon">{{removeArticle}}</i></a> 
{{/if}} 

isAuthor:function(){ 
    if(this.author === Meteor.userId()){ //this.author is author in doc 
    return true; 
    } 
    else{ 
    return false; 
    } 

編輯

如果你不及格任何數據上下文模板,然後在助手

isAuthor:function(){ 
     //here some how you need the id of the document 
     //I saw in your numlikes helper this._id returning the current doc id use the same inplace of id in the following query selector 
    //before continue confirm whether you are getting _id or not by log to console 
     var article= Article.findOne({_id:this._id}); 
     if(article && article.author === Meteor.userId()){ //this.author is author in doc 
     return true; 
     } 
     else{ 
     return false; 
     } 
+0

試過了。不工作:(感謝回覆,雖然 –

+0

可以發佈所有相關html廣告js代碼嗎? – Sasikanth

+0

可以在比較和發佈結果之前試用console.log(this.author) – Sasikanth

0

流星幫手,您將無法訪問模板對象與this referece

所以,你需要如下

    修改代碼
  • 傳遞author來幫手從模板的功能,如如下 -

    {{#if isAuthor author}} 
         <a class="remove"><i style="color:#d95c5c;" class="remove icon">{{removeArticle}}</i></a> 
    
        {{/if}} 
    

觀察如何筆者從模板傳遞到isAuthor現在

  • ,使從JavaScript來輔助函數變化
isAuthor: function(author){ 
if (this.author === author) 
    return true 
else 
    return false 
} 
+0

這也行不通。沒有顯示。呃這個應用程序是禁忌的!刪除按鈕顯示所有的時間,但我希望它是作者特定的,這就是爲什麼我需要這個功能。 –