Search code examples
meteormeteor-helper

Meteor helper function for returning the isAuthor function


I'm trying to implement something similar to TelescopeJS but a lot easier. So I just wanted to know if there's a way to check the author's Id.

What I want to do, is show the Delete button for only the author of the Article. My article has these fields in the collection:

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

I want the remove button to be shown only to the author of the post. So I need a helper function for it that would return a boolean. The function should follow : if the current user id is equal to the author's user id, then return true, otherwise false. Now it's a fairly easy function but I just dunno how to call the author field in my collection.

Thanks in advance!

JS CODE:

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

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

Solution

  • Add the isAuthor field to each article with transform. Like this.

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