Ember.js 路由器对控制器的操作 [英] Ember.js Router Action to Controller

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

问题描述

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

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

示例如下:http://jsfiddle.net/KvJ38/3/

在我的个人资料下有两个操作:一个是在状态上定义的,并且正在工作两个是在控制器上定义的.我怎样才能使它起作用,还是应该使用其他方法?

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"中添加二"功能.

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天全站免登陆