wry*_*ych 28 javascript ember.js ember-router
从路线访问控制器有什么万无一失的方法吗?
<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'
Run Code Online (Sandbox Code Playgroud)
someAction非常通用,我认为ApplicationRoute是最好的选择.
mav*_*ein 29
我认为这个方法controllerFor
应该在这个事件中可用:
App.ApplicationRoute = Ember.Route.extend
events:
someAction: (user) ->
console.log this.controllerFor("currentUser").get("name")
Run Code Online (Sandbox Code Playgroud)
针对评论中的问题进行更新:
这一切都取决于你想做什么.在这样一个基本方法上担心DRY,没有多大意义.
在你的荣誉左翼我会这样做:
App.ApplicationRoute = Ember.Route.extend
events:
someAction: (user) ->
this.controllerFor("currentUser").decrementKudos();
// implement the decrementKudos in your controller
Run Code Online (Sandbox Code Playgroud)
但我想存储这个控制器也应该有用,如果这对你来说代码太多了:
App.ApplicationRoute = Ember.Route.extend
currentUserCon : this.controllerFor("currentUser")
events:
someAction: (user) ->
this.currentUserCon.decrementKudos();
// implement the decrementKudos in your controller
Run Code Online (Sandbox Code Playgroud)
在较新版本的ember中,您可以在路径中访问当前路径的控制器,如下所示
Ember版本2.5
currentUserCon : this.controller
Run Code Online (Sandbox Code Playgroud)
如果你想访问其他路由控制器然后使用controllerFor
方法.
this.controllerFor('path to controller'); //put path to controller as parameter.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23759 次 |
最近记录: |