What I'm trying to do: Author can delete own post.
Problem: It seems parameter param=this
is not passed inside components: I implemented a action removePost
in app/components/post-box.js
which triggers item.deleteRecord()
but this item
is undefined
when I click delete button. Please help me.
posts.hbs -> post-box.hbs -> post-box.js
app/templates/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>
app/templates/components/post-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>
app/components/post-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");
}
}
}
});
我建议在传递当前上下文时使用controller.model
代替它,但是除此之外,它在组件内部的名称不是param
,因此您应该使用
<a class="deletePost" {{action "removePost" param}}>Delete</a>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.