2016-06-28 52 views
0

用户只能删除帖子,如果他是谁发布it.However用户可以看到所有的posts.The JavaScript代码的一个是:在Blaze中检查平等吗?

var postedBy = Meteor.user(); 
var currentPost = this._id; 
Posts.insert({ 
name:postName, 
createdAt:new Date(), 
postId:currentPost, 
postedBy:postedBy 
}); 

的HTML代码是:

<template name="posts"> 
{{#if currentUser}} 
{{> addPost}} 
<ul> 
    {{#each post}} 
     <li>{{> postItem}}</li> 

    {{/each}} 
</ul> 
{{/if}} 
</template> 


<template name="postItem"> 
    <li> 
    <h4>{{name}}</h4> 
    <i>Posted by {{postedBy.username}} on {{createdAt}}</i> 

    [<a href="#" class="delete-post">Delete</a>] 

    </li> 
</template> 
<template name='addPost'> 
<input type='text' placeholder='Add post here' name='postName' id ='myinput'> 
<button class="btn btn" type="button" id='btn'>Post</button> 
</template> 

currentUser.username和postingBy.username都分别显示登录用户和发布特定帖子的用户的名称。

我正在尝试使用删除锚标记。

{{#if currentUser.username==postedBy.username}} 
[<a href="#" class="delete-post">Delete</a>] 
{{/if}} 

但它显示了在命令prompt.I错误知道这是错误的,但way.How就在我写这篇“如果”语句来检查当前用户是一个我想不出任何其他的谁发布了这篇文章? 请帮忙,因为我对流星很陌生。

回答

3

您不能在空格键中使用任意JavaScript表达式。添加辅助这样的:

Template.postItem.helpers({ 
    canDelete: function(){ 
    return Meteor.user().username === this.postedBy.username; 
    } 
}); 

然后你就可以在模板中像这样使用:

{{#if canDelete}} 
    <a href="#" class="delete-post">Delete</a> 
{{/if}} 

还要注意,而不是复制用户对象到每个岗位,推荐的方法是存储用户的编号为postedBy

+0

Thanks.I会记住这一点。 –