如何在Ember.js中仅显示帖子的作者的帖子删除按钮 [英] How to display post's delete button for only post's author in Ember.js

查看:96
本文介绍了如何在Ember.js中仅显示帖子的作者的帖子删除按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我已经被困了几天如何显示一个帖子的删除按钮,仅为Ember.js中的帖子的作者(我正在使用ember-cli来构建)。我不知道在哪里放置逻辑当悬停一个帖子(列表)时,如果该帖子的作者等于当前登录的用户,则显示删除按钮我迷路了。请帮助我。

Hello I've been stuck for days how to display a post's delete button only for the post's author in Ember.js (I'm using ember-cli to build this). I don't know where to put the logic of "When hovering a post (list), if the post's author is equal to currently logged in user, then display the delete button" I am lost. Please help me.

在模板 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;

SOLVED

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

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

推荐答案

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

As mentioned above, you'll want to use an itemController

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

然后在 itemController 中,您将创建一个计算的属性,用于根据帖子的作者ID检查用户ID

And then in the itemController you will create a computed property that checks the user id against the author id of the post

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')
})

这篇关于如何在Ember.js中仅显示帖子的作者的帖子删除按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆