2013-04-13 138 views
0

在路由器中,我有一個事件'removeComment'。在控制器中,如果通過訪問this.get('target')。send('removeComment',context);,我得到錯誤沒有處理事件'removeComment'。當我使用this.get('target.router')。send('removeComment',comment),錯誤變爲對象#沒有方法'發送'。使用this.router.send('removeComment',comment),將給出錯誤:無法讀取屬性'發送'未定義的emberjs-RC2-如何從控制器和操作訪問路由器實例不冒泡到路由器

也只是發送到'PostComitController'行動到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> 
+0

請問您可以添加路由圖嗎?我認爲this.get('target')。發送('removeComment',context)'應該可以工作,但我懷疑這是發送到其他路由。 –

+0

感謝您的時間。我在問題的頂部添加了路由器,並決定添加** [jsfiddle](http://jsfiddle.net/VrR2T/6/)**。 – brg

回答

1

感謝foir的updae,結束了小提琴,它幫助了很多:)。我想我知道這裏發生了什麼。

首先,在控制器中的destroyMe功能是不正確的,它應該是

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

然後,你在post.comments的模板調用它,但你實現它在'PostEditCommentRoute'中,這是PostCommentsRou​​te的一個子路由。因此,在PostCommentsRou​​te中提起這個事件應該能夠發揮作用。現在

,有關您的代碼的整體評論,也有一些奇怪的事情,比如

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

{{render 'post.comments' comments}} 

其結果是,當你點擊「評論」鏈接,它拋出一個錯誤(視圖已經呈現)。

也有可能被避免的路線一些代碼,例如,所有的setupController掛鉤像

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

這是默認的行爲,所以你不必重寫它。

+0

感謝sly7_7的所有反饋。我已經刪除了它們在默認情況下保留的setupController。對於導致視圖的註釋代碼已經被渲染,我已經使用#render助手刪除了該位。我正在測試兩者,而且肯定是錯誤地留下了一個未註釋的。你是對的,移動removeComment直到PostCommentsRou​​te,使事件被調用,但現在它給**新的錯誤:不能調用undefined **的方法'deleteRecord'。我正在調查爲什麼。 ** [小提琴](http://jsfiddle.net/VrR2T/14/)**。如果我解決它,我會更新你。 **感謝您的指導**。 – brg

+0

我使用調試器在控制檯中查找了'removeComment'事件。問題出現在這行:** var comment = context.get('content')**。在控制檯中,如果我鍵入** comment **,它是未定義的。但是,當我鍵入**上下文**時,我得到想要銷燬的正確對象,但未將其分配給註釋變量。 – brg

+0

是的,我認爲上下文是指你傳遞給send方法的對象,所以它是評論本身。你應該可以在其上調用deleteRecord。 –