在路由器中,我有一个事件“removeComment”。在控制器中,如果通过this.get('target').send('removeComment', context)访问它,我会收到错误Nothing handlered the event 'removeComment'。当我使用this.get('target.router').send('removeComment', comment)时,错误变为Object # has no method 'send'。使用this.router.send('removeComment', comment),会报错:Cannot read property 'send' of undefined。
同样,仅将“removeComment”操作发送到 PostEditController 不会通过控制器冒泡,直到路由。
如何从 emberjs rc2 和 routerV2 中的控制器访问路由器实例。
jsfiddle _
路由器:
EmBlog.Router.map(function() {
this.resource("posts", {path: '/posts'}, function(){
this.route('new');
this.resource('post', {path: '/:post_id/'}, function(){
this.route('edit', {path: '/edit'});
this.route('comments', {path: '/comments'});
this.route('newComment');
this.route('comment', {path: '/comments/:comment_id'});
this.route('editComment', {path: '/comments/:comment_id/edit'});
});
});
});
控制器
EmBlog.PostEditCommentController = Ember.ObjectController.extend({
destroyMe: function(comment) {
this.get('target.router').send('removeComment', comment);
}
});
路由器
EmBlog.PostEditCommentRoute = Ember.Route.extend({
events: {
removeComment: function(context) {
var comment = context.get('content');
comment.deleteRecord();
comment.get('store').commit();
this.transitionTo('post.index');
}
}
});
我在帖子/评论模板中访问它。这是该模板的控制器。
EmBlog.PostCommentsController = Ember.ArrayController.extend({
needs: ['postEditComment']
});
帖子/评论模板
<script type="text/x-handlebars" data-template-name="post/comments">
{{#each controller}}
<p><a href='#' {{action destroyMe this target="controller.controllers.postEditComment"}}> Destroy </a></p>
{{/each}}
</script>