Ember.js组件 - param不会在组件内部传递

时间:2014-05-30 02:15:05

标签: ember.js components

我正在尝试做什么: 作者可以删除自己的帖子。

问题: 似乎参数param=this未在组件内部传递: 我在removePost中实施了一项操作app/components/post-box.js 当我点击删除按钮时触发item.deleteRecord()itemundefined。请帮帮我。

posts.hbs - > post-box.hbs - >后box.js

应用/模板/ posts.hbs

<div class="postsContainer">
  <div class="innerPostsContent">
    {{#each itemController="post"}}
      {{!-- Here is the problem: with components, param doesn't get this --}}
      {{post-box user=user body=body date=date isAuthor=isAuthor view=view action="removePost" param=this session=session}}
    {{/each}}
  </div>
</div>

应用/模板/组件/后box.hbs

<div class="eachPost">
  <div class="eachPostContent" {{action 'showDelete'}}>
    <p class="postAuthor"><strong>{{user.id}}</strong></p>
    <p class="postContent">{{body}}</p>
    <span class="timePosted"><em>{{format-date date}}</em></span>
      {{#if isAuthor}}   
        {{!-- this part leads next file: app/components/post-box.js --}}
        <a class="deletePost" {{action "removePost" this}}>Delete</a> 
      {{/if}}
  </div>
  {{/view}}
</div>

应用/组件/后box.js

export default Ember.Component.extend({
  actions: {
    removePost: function(item) {
      if(this.get('session.user') && item.get('user') === this.get('session.user')){
        item.deleteRecord(); // Error: Uncaught TypeError: undefined is not a function 
        item.save();
      } else {
        console.log("You are not this post's author");
      }
    }
  }
});

1 个答案:

答案 0 :(得分:1)

我建议在传递当前上下文时使用controller.model而不是这个,但除此之外,它在组件中的名称是param而不是这个,所以你应该使用

<a class="deletePost" {{action "removePost" param}}>Delete</a>