【问题标题】:Can I call other controller's method in Ember?我可以在 Ember 中调用其他控制器的方法吗?
【发布时间】:2013-10-10 08:18:50
【问题描述】:

在我的 Ember 应用程序中,我有一个 ArrayController,我在其中创建了一个方法:

App.SwatchesController = Ember.ArrayController.extend({
  push: function (color) {
    var model = this.get('model');
    /* do a few things */
    this.set('model', model);
  }
});

当我尝试从ApplicationCotroller 中的操作调用此方法时,我得到一个TypeError

App.ApplicationController = Ember.Controller.extend({
  actions: {
    parse: function () {
      App.SwatchesController.push('#fff'); // TypeError: Object function (){/* long body here */} has no method 'push' 
    }
  }
});

我应该在 Ember 中使用控制器方法吗?不幸的是,官方文档仅提供了有限的控制器示例。

我可能可以将模型定义为App.Swatches 并直接从ApplicationController 管理它,但我相信我不应该这样做,因为SwatchesController 的用途是。

演示:http://jsbin.com/UToDazI/3/edit?html,js,output

【问题讨论】:

    标签: javascript model-view-controller ember.js


    【解决方案1】:

    您可以尝试在ApplicationController 中使用以下代码吗?

    App.ApplicationController = Ember.Controller.extend({
        needs: "swatches",
        swatchesController: Ember.computed.alias("controllers.swatches"),
        actions: {
            parse: function () {
                var swatchesController = this.get('swatchesController');
                swatchesController.push('#fff');
            }
        }
    });
    

    提到了管理控制器之间的依赖关系here

    【讨论】:

    • 得到了ReferenceError: swatchesController is not defined,尝试了this.swatches.Controller - 一样。
    • 道歉。我刚刚在我的答案中编辑了代码——我正确地引入了变量swatchesController。让我知道这是否有效。
    • 在 Edu 的回答中,我在尝试将模型放入 push 时得到了 TypeError: Object [object global] has no method 'get'
    • 错误指向哪一行?这可能会更清楚地说明这个问题。
    • var model = this.get('model'); 我已经在我的问题中添加了一个演示,也许它会有所帮助。
    【解决方案2】:

    此代码基于 Jackil 的响应

    App = Ember.Application.create();
    
    App.ApplicationController = Ember.Controller.extend({
      source:null,
      needs: "swatches",
      swatchesController: Ember.computed.alias("controllers.swatches"),
      actions: {
        parse: function () {
          var source = this.get('source');
          source.split(',').forEach(function (color) {
            this.get('swatchesController').addObject({color:color.trim()});
          },this);
        }
      }
    });
    
    App.SwatchesController = Ember.ArrayController.extend({});
    

    模板也修改了

    <script type="text/x-handlebars" data-template-name="swatches">
    <ul>
      {{#each}}
        <li>{{color}}</li>
      {{/each}}
    </ul>
    </script>
    

    两点:

    • 一般Js,里面forEach的作用域有变化,可以发第二个参数设置this变量,TypeError: Object [object global] has no method 'get ' 在 forEach 中...
    • 在 ember 中最好使用提供的 api 函数,在数组控制器中我们有 addObject,这样会通知绑定并相应地更新布局

    JsBinhttp://jsbin.com/UToDazI/6/

    【讨论】:

    • 现在我明白了。非常感谢你没有放弃。我认为如果我接受 Jackell 的回答是公平的,但我计划在符合条件的情况下奖励您的回答。
    【解决方案3】:

    您应该在调用其方法之前实例化控制器...在您的代码中 App.SwatchesController 是一个从其他类扩展的类,而不是实例...

    有点像

    App.swatchesController = App.SwatchesController.create({});
    

    然后你可以调用实例化控制器上的方法,非 capilazed 的......

      parse: function () {
         App.swatchesController.push('#fff'); 
      }
    

    【讨论】:

    • 这样,当我尝试获取model 属性时,我得到了TypeError: Object [object global] has no method 'get'
    • 然而,要走的路是@Jackail 在他的回复中建议的,是让框架实例化控制器、视图等的 ember 方法......
    • 我同意您的 Edu 评论,您应该让框架为您处理对象的实例化。下面是 Yehuda 对容器的简短说明(Ember 的依赖总部):stackoverflow.com/questions/14085749/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多