如何从Ember中的路由访问控制器? [英] How to access controller from route in Ember?

查看:143
本文介绍了如何从Ember中的路由访问控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < a href =#class = btn{{actionsomeActionuser}}> add< / a> 

App.ApplicationRoute = Ember.Route.extend
事件:
someAction:(user) - >
console.log'给我的名字从currentUser控制器'

someAction非常一般,我认为ApplicationRoute是它的最佳选择。

解决方案

我认为方法 controllerFor 应该在这个事件中可用:

  App.ApplicationRoute = Ember.Route.extend 
事件:
someAction:(user) - >
console.log this.controllerFor(currentUser)。get(name)

更新,以回应这些行动中的问题:



这一切都取决于你想做什么。在这样一个基本的方法上担心干,,,,,,,$ $。。。。。。。。。。。。>>>>>>>> $ $ $ $ $ $ $ $ $ $ $ $:::::::: b $ b

  App.ApplicationRoute = Ember.Route.extend 
事件:
someAction:(user) - >
this.controllerFor(currentUser)。decrementKudos();
//在控制器中实现reducementKudos

但是我想存储这个控制器也应该工作,如果这是太多的代码:

  App.ApplicationRoute = Ember.Route.extend 
currentUserCon: this.controllerFor(currentUser)
事件:
someAction:(user) - >
this.currentUserCon.decrementKudos();
//在你的控制器中实现递减Kudos


Is there any foolproof way to access controller from a route?

<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'

The someAction is very general and I think that ApplicationRoute is the best place for it.

解决方案

I think the method controllerFor should be available in this event:

App.ApplicationRoute = Ember.Route.extend
  events:
    someAction: (user) ->
      console.log this.controllerFor("currentUser").get("name")

Update in response to the questions in the commments:

It all depends on what you want to do. Worrying about DRY on such a basic method, does not make much sense imho.

In your kudos left case I would do this:

App.ApplicationRoute = Ember.Route.extend
  events:
    someAction: (user) ->
      this.controllerFor("currentUser").decrementKudos();
      // implement the decrementKudos in your controller

But I guess storing this one controller should also work, if this is too much code for you:

App.ApplicationRoute = Ember.Route.extend
  currentUserCon : this.controllerFor("currentUser")
  events:
    someAction: (user) ->
      this.currentUserCon.decrementKudos();
      // implement the decrementKudos in your controller

这篇关于如何从Ember中的路由访问控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆