【问题标题】:emberjs-RC2- how to access router instance from controller & actions don't bubble to routerember js-RC2-如何从控制器访问路由器实例并且操作不会冒泡到路由器
【发布时间】: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 中的控制器访问路由器实例。

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

我在 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


【解决方案1】:

感谢更新,结束小提琴,它有很大帮助:)。我想我知道这里发生了什么。

首先,控制器中的destroyMe功能不对,应该是真的

destroyMe: function(comment) {
  this.get('target').send('removeComment', comment);
}

然后,您在 post.cmets 的模板中调用它,但您在 PostCommentsRou​​te 的子路由“PostEditCommentRoute”中实现它。因此,在 PostCommentsRou​​te 中拉起事件应该可以使其工作。

现在,作为对您的代码的整体评论,有一些奇怪的东西,例如

<p>{{#linkTo 'post.comments'}}  comments{{/linkTo}}</p>  

{{render 'post.comments' comments}}

因此,当您单击“cmets”链接时,会引发错误(视图已呈现)。

路由中还有一些可以避免的代码,例如所有 setupController 钩子,例如

setupController: function(controller, model){
  controller.set('content', model);
}

这是默认行为,因此您不必覆盖它。

【讨论】:

  • 感谢 sly7_7 的所有反馈。我已经删除了他们坚持使用默认设置的 setupController。对于导致视图的注释代码已经呈现,我已经使用#render 助手删除了该位。我正在测试两者,并且一定是错误地遗漏了一个未注释的内容。你是对的,将 removeComment 移到 PostCommentsRou​​te,使事件被调用,但现在它给出了 新错误:无法调用未定义的方法 'deleteRecord'。我正在调查原因。 fiddle。如果我解决它,我会更新你。 感谢您的指导
  • 我已经使用调试器在控制台中查找了“removeComment”事件。问题在于这一行:var comment = context.get('content')。如果我在控制台中输入 comment 它是未定义的。但是当我输入 context 时,我得到了我想要销毁的正确对象,但它没有被分配给评论变量。
  • 是的,我认为上下文是指您传递给发送方法的对象,所以它是评论本身。你应该可以调用 deleteRecord 了。
  • 感谢您的好意以及您花时间帮助我解决此问题。有时似乎要解决一个问题,需要取消调试器语句。祝您愉快。干杯
  • 很高兴为您提供帮助 :) 我希望您能继续使用 Ember.js :)
猜你喜欢
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多