从其他控制器获取属性

时间:2013-09-26 15:27:56

标签: ember.js

在我的Ember应用程序的AuthController中,我设置了一个currentUser,我可以从AuthController中获取

this.get('currentUser');

来自AuthController。在另一个控制器中,我使用needs: ['auth'],以便我可以从auth控制器获取currentUser变量,但它不起作用。我该怎么做?

App.BlobController = Ember.ObjectController.extend({
     needs: ['auth'],

    actions: {

    doSomething: function() {
       var user;
       user = this.get('currentUser'); /// not working
       $.post(".....

更新

按照Ember文档中有关管理控制器http://emberjs.com/guides/controllers/dependencies-between-controllers/之间的依赖关系的说明,我也尝试controllers.auth.get('currentUser');,但它不起作用

doSomething: function() {
           var user;
           user = controllers.auth.get('currentUser'); /// not working
           $.post(".....

1 个答案:

答案 0 :(得分:4)

它的工作方式如下:

App.BlobController = Ember.ObjectController.extend({
  needs: ['auth'],
  actions: {
    doSomething: function() {
      var user;
      user = this.get('controllers.auth.currentUser');
      $.post("...

或更清晰地声明BlobController上的计算别名,该别名引用AuthController currentUser属性:

App.BlobController = Ember.ObjectController.extend({
  needs: ['auth'],
  currentUser: Ember.computed.alias('controllers.auth.currentUser'),
  actions: {
    doSomething: function() {
      var user;
      user = this.get('currentUser');
      $.post("...

希望它有所帮助。