0

我正在尝试实现类似于 TelescopeJS 的东西,但要容易得多。所以我只是想知道是否有办法检查作者的身份。

我想要做的是只为文章的作者显示删除按钮。我的文章在集合中有这些字段:

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

3 回答 3

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;
      }
于 2015-01-10T12:10:40.617 回答
0

this在 Meteor 助手中,您将无法通过参考访问模板对象

所以,你需要修改代码如下

  • 从模板传递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
}
于 2015-01-10T15:02:30.307 回答
0

使用转换将 isAuthor 字段添加到每篇文章。像这样。

// the articles helper
articles: function(){
  var userId = Meteor.userId();
  return Articles.find({},{transform: function (doc ){
    doc.isAuthor = doc.author === userId; 
    return doc;
  }});
}
于 2015-01-10T20:08:04.653 回答