Ember.js路由器动作到控制器 [英] Ember.js Router Action to Controller

查看:73
本文介绍了Ember.js路由器动作到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Ember路由器时,如何在模板中定义与控制器相连的操作?

When I use the Ember Router, how can I define actions in the template who are connected to the controller?

一个示例在这里:
< a href =http://jsfiddle.net/KvJ38/3/ =nofollow> http://jsfiddle.net/KvJ38/3/

Unter我的配置文件是两个动作:
一个在状态上定义,并且正在工作
两个在Controller上定义。我如何使这项工作或应该使用其他方法?

Unter My Profile are two actions: One is defined on the State, and is working Two is defined on the Controller. How can i make this working or should I use another approach?

App.Router = Em.Router.extend({
  enableLogging: true,
  location: 'hash',

  root: Em.State.extend({
    // EVENTS
    goHome: Ember.State.transitionTo('home'),
    viewProfile: Ember.State.transitionTo('profile'),

    // STATES
    home: Em.State.extend({
      route: '/',
      connectOutlets: function(router, context) {
        var appController = router.get('applicationController');
        appController.connectOutlet(App.HomeView);
      }
     }),

    // STATES
    profile: Em.State.extend({
      route: '/profile',
        connectOutlets: function(router, context) {
          var appController = router.get('applicationController');
          appController.connectOutlet(App.ProfileView);
        }
    }),

    one: function() {
      alert("eins");
    },
  }) 
});


推荐答案

动作的默认目标是路由器,但您可以在模板中定义另一个模板:

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

{{action two target="controller"}}

并在App.ProfileController中添加two功能。

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

更新

此答案希望在2012年年中正确。现在(2014年9月),文档说:

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

默认情况下, {{action}} 帮助器触发模板控制器上的一个方法。
[...]如果控制器没有实现与其操作对象中的操作具有相同名称的方法,则该操作将被发送到路由器,其中当前活动的叶子路由将被给予机会处理动作。 [...]如果模板的控制器和当前活动的路线都没有实现一个处理程序,那么动作将继续浮动到任何父路由。最终,如果定义了 ApplicationRoute ,它将有机会处理该操作。当触发一个动作,但在控制器上没有实现匹配的动作处理程序时,将抛出当前路由或任何当前路由的祖先错误。

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.

这篇关于Ember.js路由器动作到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆