您好我已经被困了好几天了,如何在 Ember.js 中仅为帖子的作者显示帖子的删除按钮(我正在使用 ember-cli 来构建它)。我不知道在哪里放置“悬停帖子(列表)时,如果帖子的作者等于当前登录的用户,则显示删除按钮”的逻辑我迷路了。请帮我。
在模板中app/templates/posts.hbs
{{#each}}
<div class="eachPost">
{{#view 'posts'}}
<div class="postProfilePhoto">
{{#link-to 'users' }}
<img src="" alt="Profile Photo">
{{/link-to}}
</div>
<div class="eachPostContent">
<p class="postAuthor"><strong>{{user.id}}</strong></p>
<p class="postContent">{{body}}</p>
<span class="timePosted"><em>somtimes ago</em></span>
{{#if view.entered}}{{#if isAuthor}}
<a class="deletePost" {{action "removePost" this}}>Delete</a>
{{/if}}{{/if}}
</div>
{{/view}}
</div>
{{/each}}
在视图中app/views/posts.js
var Posts = Ember.View.extend({
classNames: ['eachPostContent'],
mouseEnter: function(event){
this.set('entered', true);
this.get('controller').send('isAuthor', this.get('post').user);
},
mouseLeave: function(){
this.set('entered', false);
}
});
export default Posts;
在控制器中app/controllers/posts.js
var PostsController = Ember.ArrayController.extend({
...
isAuthor: function(user){
if(this.get('session').user !== null && user === this.get('session').user){
return true;
} else {
return false;
console.log('You are not author');
}
}
}
});
export default PostsController;
解决了
在app/templates/posts.hbs
{{#each itemController="post"}}
<div class="eachPost">
创建app/controllers/post.js
var PostController = Ember.ObjectController.extend({
isAuthor: function(){
return this.get('user') === this.get('session').user;
}.property('user')
});
export default PostController;
删除以下代码app/views/posts.js
this.get('controller').send('isAuthor', this.get('post').user);
并从中删除isAuthor
功能app/controllers/posts.js