2016-06-28 24 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會記住這一點。 –