有没有一种万无一失的方法可以从路由访问 Controller ?
<a href="#" class="btn" {{action "someAction" user}}>add</a>
App.ApplicationRoute = Ember.Route.extend
events:
someAction: (user) ->
console.log 'give me name from currentUser controller'
someAction 非常通用,我认为 ApplicationRoute 是最适合它的地方。
最佳答案
我认为方法 controllerFor
应该在此事件中可用:
App.ApplicationRoute = Ember.Route.extend
events:
someAction: (user) ->
console.log this.controllerFor("currentUser").get("name")
更新以回应评论中的问题:
这完全取决于您想要做什么。在这样一个基本方法上担心 DRY 没有多大意义,恕我直言。
在你的荣誉左案例中,我会这样做:
App.ApplicationRoute = Ember.Route.extend
events:
someAction: (user) ->
this.controllerFor("currentUser").decrementKudos();
// implement the decrementKudos in your controller
但我想存储这个 Controller 也应该可以工作,如果这对您来说代码太多:
App.ApplicationRoute = Ember.Route.extend
currentUserCon : this.controllerFor("currentUser")
events:
someAction: (user) ->
this.currentUserCon.decrementKudos();
// implement the decrementKudos in your controller
关于javascript - 如何从 Ember 中的路由访问 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15911704/