动作冒泡在Ember [英] Action bubbling in Ember

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

问题描述

如果这个问题的答案很明显,那么我比较新的Ember呢,原谅我。我最近开始了一个新工作,我认为他们采取行动冒泡的方法是好奇的。



这是一个很大的应用程序,有很多深入的嵌套组件。要发挥控制器的作用,他们一直在使用这样的事情:

  actionName(parameter){
this.attrs.actionName(parameter);
},

然后,这会将一个级别上升到下一个组件,再次呼叫相同的动作。这将继续下去,直到它到达控制器的行动被定义。



我不是因为各种原因的粉丝,但主要是因为它使写作任何新的动作都是一种烦恼。



我的问题是一个两面包:



1)对于深层次的组件有没有更好的解决方案?



2)我以前看过 sendAction ,但实际上没有使用。 this.attrs.actionName(parameter);



谢谢!

解决方案


对于深度嵌套的组件,是否有更好的解决方案?




简短的答案是否定的。 Ember组件强制您明确鼓泡操作。消费者还必须明确订阅一个接收它的动作。这些设计决策是为了避免意外的错误,使代码尽可能明确。 (想象一下,如果事件自动冒泡,一个组件添加了一个新的事件,这些事件会自动发生泡沫,没有被捕获,然后导致错误。)


我以前看过sendAction,但实际上没有使用过。 this.attrs.actionName(参数);?


sendAction 是旧的做事方式(前2.0)。在Ember 2.0中,动作被放置在 attrs 对象中。这种方法有一些好处,但最大的是可读性。您应该更喜欢新的方法,特别是因为新的方法允许操作将值返回给调用者(某些 sendAction 不允许)。



为了使一个很长的故事更短:看起来很冗长,但这只是Ember做的事情。通常情况下,它比替代品好得多。


I am relatively new to Ember so forgive me if the answer to this question is obvious. I started a new job recently and I thought their approach to action bubbling was...curious.

This is a big application with a lot of deeply nested components. To bubble actions up to the controller, they have been using this sort of thing a lot:

actionName (parameter) {
    this.attrs.actionName(parameter);
},

This will then bubble one level up to the next component, where they will call the same action again. This will continue in such a way until it reaches the controller where the action is defined.

I am not a fan of this for various reasons, but mostly because it makes writing any new actions a chore.

My question is a two-parter:

1) For deeply nested components, is there a better solution?

2) I have seen sendAction before, but have not used it in practice. What is the difference between this.attrs.actionName(parameter);?

Thank you!

解决方案

For deeply nested components, is there a better solution?

The short answer is no. Ember components force you to explicitly bubble actions. The consumer must also explicitly "subscribe" to an action to receive it. These design decisions were made so that code is as explicit as possible to avoid unintended errors. (Imagine if events automatically bubbled and a component added a new event. Those events would automatically bubble, go uncaught, then cause an error.)

I have seen sendAction before, but have not used it in practice. What is the difference between this.attrs.actionName(parameter);?

sendAction is the old way of doing things (pre 2.0). In Ember 2.0 actions are placed in the attrs object instead. There's a few benefits to this approach, but the biggest one is readability. You should prefer the new method going forward, especially since the new method allows actions to return values to the caller (something that sendAction doesn't allow).

To make a really long story somewhat shorter: it seems very verbose, but it's just the way Ember does things. More often than not, it's much better than the alternatives.

这篇关于动作冒泡在Ember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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