我正在编写一个消息传递应用程序,它为用户提供了已提交的给定消息的删除/编辑功能。我想做的是写点:
{{#if currentUser._id === this._id}}
<!-- Show -->
{{/if}}
但这可能是错误的我为消息记录编写了一个模板:
<template name="message">
<div class="row message-row">
<div class="col-md-12">
<div class="message-container">
<div class="message-avatar">
<img src="{{userAvatar}}">
</div>
<p>{{message}}</p>
<div class="message-time">{{prettifyDate time}}</div>
<!-- this is the div to hide / show based on the conditional -->
<div class="message-controls">
<button class="btn btn-link btn-xs" type="button" id="deleteMessage"><i class="fa fa-trash-o"></i></button>
<button class="btn btn-link btn-xs" type="button" id="editMessage"><i class="fa fa-edit"></i></button>
</div>
<!-- end -->
</div>
</div>
</div>
</template>
我在client.js中使用以下内容
Template.messages.messages = function() {
return Messages.find({}, { sort: {time: -1} });
}
但此时我被困了
答案 0 :(得分:4)
假设您的邮件文档包含userId
字段,您只需执行以下操作:
Template.message.helpers({
isOwner: function() {
return this.userId === Meteor.userId();
}
});
和
{{#if isOwner}}
<!-- controls -->
{{/if}}
您还可以为此创建一个更灵活,可重用的全局帮助程序:
Template.registerHelper('isCurrentUser', function(userId) {
return userId === Meteor.userId();
});
<!-- in this example, the document might have an `ownerId` field rather than `userId` -->
{{#if isCurrentUser ownerId}}{{/if}}
当然,您还需要使用allow/deny API或自定义方法验证服务器上的更新。