在{{action}} helper中调用使用替代控制器

Call use alternative controller in {{action}} helper

本文关键字:控制器 action helper 调用      更新时间:2023-09-26

是否可以从车把模板调用另一个控制器方法?

<script type="text/x-handlebars" data-template-name="home">
    <a {{action logout target="Application.loginController"}}>
        logout
    </a>
</script>
误差

Uncaught TypeError: Cannot read property 'send' of undefined ember.js:25757
    (anonymous function) ember.js:25757
    (anonymous function) ember.js:4509
    Ember.handleErrors ember.js:411
    invoke ember.js:4507
    tryable ember.js:4692
    Ember.tryFinally ember.js:1206
    Ember.run ember.js:4696
    ActionHelper.registeredActions.(anonymous function).handler ember.js:25756
    (anonymous function) ember.js:14452
    Ember.handleErrors ember.js:411
    (anonymous function) ember.js:14444
    b.event.dispatch jquery.js:3
    v.handle jquery.js:3

浏览器:Chrome Version 26.0.1410.63

灰烬:1.0.0-rc.3

车把:1.0.0-rc.3

Jquery: 1.9.1

您可以使用Ember的需求功能来完成此操作。

你指定一个控制器"需要"访问另一个控制器,然后Ember允许你在controllers.controllerName:

访问它
App.ApplicationController = Ember.Controller.extend({
  applicationAction: function() {
    console.log("action in application controller");
  }
});
App.IndexController = Ember.ArrayController.extend({
  needs: ['application'],
  indexProperty: "index controller"
});

可以在模板中访问:

<a href='#' {{action applicationAction target="controllers.application"}}>
    logout
</a>

JSBin示例