【发布时间】:2013-04-13 19:37:37
【问题描述】:
在路由器中,我有一个事件“removeComment”。在控制器中,如果通过 this.get('target').send('removeComment', context); 访问它,我会收到错误 Nothing handling 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 中的控制器访问路由器实例。
路由器:
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');
}
}
});
我在 post/cmets 模板中访问它。这是该模板的控制器。
EmBlog.PostCommentsController = Ember.ArrayController.extend({
needs: ['postEditComment']
});
post/cmets 模板
<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>
【问题讨论】:
-
你能添加你的路由图吗?我认为 this.get('target').send('removeComment', context)` 肯定可以工作,但我怀疑这是发送到其他路线。
-
感谢您的宝贵时间。我已在问题顶部添加了路由器,并决定还添加一个 jsfiddle。
标签: ember.js ember-router