抓住所有动作处理程序 [英] Catch All Action Handler

查看:88
本文介绍了抓住所有动作处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,这真的是一个问题,这个问题是从这个。这里有更多的信息,但我猜,最好问一个直接的具体问题。所以这里:

Now, this is really a question that's evolved from this. There is a whole lot more info there, but I guess, it's better to ask a direct specific question. So here it goes:

我们通常在控制器中定义如下操作:

We typically define actions in a controller like so:

var FooController = Ember.Controller.extend({
    actions: {
        login: function() {
        }
    }
});

是否有一种方法来定义捕获所有操作处理程序,像这样(假设):

Is there a way to define a catch all action handler, like so (hypothetically):

actions: {
    login: function() {
    },
    *: function(actionName, paramArray) {
    }
}

这将类似于余烬href =https://github.com/emberjs/ember.js/pull/1465 =nofollow noreferrer>捕获我相信已经实现的所有路由,尽管我还没有尝试过。

This would be analogous to embers catch all route which I believe has been implemented, though I haven't tried it.

我需要这个,因为我的 Ember.Component 呈现一个用户提供的 {部分}}的部分模板使用.com / guides / templates / rendering-with-helpers /#toc_the-code-partial-code-helperrel =nofollow noreferrer 帮手。这个部分可能在它们中指定了 {{action}} 。这些动作不会引起呼叫控制器或路由,并在组件中丢失。这个事实在第4段的 docs 中提及。

I need this because my Ember.Component renders a user supplied partial template using the {{partial}} helper. This partial might have {{action}}'s specified in them. These actions don't bubble up to the calling controller or route and are lost inside the component. This fact is mentioned in the docs in para 4.

如果可以捕获所有操作,我的组件可以实现它,并使用以下操作将操作发回给调用者:

If a catch all action was possible, my component could implement it and send the action back to the caller using something like this:

actions: {
    *: function(actionName, paramArray) {
        this.sendAction(actionName, paramArray)
    }
}


推荐答案

actions 控制器,视图和路由上的哈希由操作处理程序mixin [1]提供。正如你所看到的,它注入 send(actionName)方法,并使得一个对象能够接收动作。您现在可以轻松地覆盖此函数并捕获所有操作,而不是在操作哈希中查找它,这是实现所执行的。

The functionality around the actions hash on controllers, views and routes is provided by the action handler mixin [1]. As you can see, it injects the send(actionName) method and makes enables an object to receive actions. You can now easily override this function and catch all actions instead of looking for it in the actions hash -- which is what the implementation does.

[1] https://github.com/emberjs/ember.js/blob/v1.3.0/packages/ember-runtime/lib/mixins/action_handler.js#L8

这篇关于抓住所有动作处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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