如何在 Ember 中仅显示帖子作者的帖子删除按钮.js



你好,我已经被困了好几天,如何显示一个帖子的删除按钮,仅为该帖子的作者在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;

in app/templates/posts.hbs

{{#each itemController="post"}}
  <div class="eachPost">

create 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);

并从app/controllers/posts.js中删除isAuthor函数

如上所述,您将需要使用itemController

var PostsController = Ember.ArrayController.extend({
   itemController:'post',
    ...
});

然后在itemController中你将创建一个计算属性来检查用户id和文章的作者id

var PostController = Ember.ObjectController.extend({
    isAuthor: function(){
      //assuming the user property exists on an individual post model
      return this.get('user') === this.get('session').user;
    }.property('user')
})

相关内容

  • 没有找到相关文章

最新更新