检查Blaze中的平等?

时间:2016-06-28 14:35:54

标签: javascript meteor meteor-blaze meteor-accounts

用户只能删除帖子,如果他是发布的帖子。但是用户可以看到所有帖子.javascript代码是:

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和postedBy.username分别显示登录用户的名称和分别发布特定帖子的用户。

我正在尝试使用Delete锚标记。

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

但它在命令提示符中显示错误。我知道它是错误的但我想不出任何其他方式。我如何写这个'if'语句来检查当前用户是否是发布此帖子的用户? 请帮助我,因为我是Meteor.Sorry的新手,因为英语不好。

1 个答案:

答案 0 :(得分: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中,而不是将用户对象复制到每个帖子中。

相关问题