Ember.js Router Action to Controller

只谈情不闲聊 提交于 2019-11-30 07:33:07

The default target of an action is the router, but you can define another one in the template:

{{action two target="controller"}}

And add a "two" function in "App.ProfileController".

UPDATE

This answer was hopefully correct mid 2012. Now (September 2014), the documentation says:

By default, the {{action}} helper triggers a method on the template's controller. [...] If the controller does not implement a method with the same name as the action in its actions object, the action will be sent to the router, where the currently active leaf route will be given a chance to handle the action. [...] If neither the template's controller nor the currently active route implements a handler, the action will continue to bubble to any parent routes. Ultimately, if an ApplicationRoute is defined, it will have an opportunity to handle the action. When an action is triggered, but no matching action handler is implemented on the controller, the current route, or any of the current route's ancestors, an error will be thrown.

You can specify the target attribute explicitly, as noted by @Stéphane, in order to send the action elsewhere.

If unspecified, the target of an action helper is the controller.target. As you noted, this is usually set to the router.

If you have a template where you want the default target be different, you can make that happen by setting the target property of the controller. For example, to set the target to the controller itself:

App.MyController = Ember.Controller.extend({
  init: function(){
    this._super();
    this.set('target', this);
  };
});

The controller should not be "directly" in charge of action event. The state/route is.

I believe https://github.com/emberjs/ember.js/issues/1015 will help you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!