0

在我的 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(".....
4

1 回答 1

4

它的工作方式如下:

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

或者更干净地在 which 上声明一个计算的别名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("...

希望能帮助到你。

于 2013-09-26T15:52:50.220 回答