聚合物中的横向交流 [英] lateral communication in Polymer

查看:65
本文介绍了聚合物中的横向交流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在于:

任何两个Polymer元素都可能需要进行通信。没有假设这些元素在DOM(或影子DOM)中的位置,这意味着一个事件不能简单地冒泡到另一个元素。

Any two Polymer elements may need to communicate. No assumption is made as to where these elements might be in the DOM (or shadow DOM), this means one event cannot simply bubble up to another element.

好老实现这一目标的方法是让事件冒泡到根节点,然后在根节点上激活广播事件,以便侦听其他元素。

The good old way to achieve this would have been to let events bubble up to the root node and then fire broadcast events on the root node for other elements to listen to.

然而,这种方法打破了封装,似乎违背了Polymer的整体设计。例如,AngularJS提供了一个事件广播器,可以防止控制器不必要地保留对根节点的引用。

This approach however breaks encapsulation and seems to go against Polymer's overall design. AngularJS for example provides an event broadcaster that keeps controllers from unnecessarily keeping references to the root node.

使用Polymer可以实现这种方法吗?否则可以用不同的方法解决这个问题吗?

Can such approach be achieved with Polymer? Otherwise can this be solved with a different approach?

推荐答案

你应该可以使用聚合物来做到这一点-signals

http://www.polymer-project.org/articles/communication.html#using-ltpolymer-signalsgt

从文档中引用:


你的元素触发聚合物信号并在其有效载荷中命名信号:

Your element fires polymer-signal and names the signal in its payload:



this.fire('polymer-signal', {name: "foo", data: "Foo!"});




此事件冒泡到文档处理程序构造和分派新内容的位置事件,聚合物信号foo,对所有的实例。您的应用程序或其他Polymer元素的部分可以声明一个元素来捕获命名信号:

This event bubbles up to document where a handler constructs and dispatches a new event, polymer-signal-foo, to all instances of . Parts of your app or other Polymer elements can declare a element to catch the named signal:



<polymer-signals on-polymer-signal-foo="{{fooSignal}}"></polymer-signals>

这篇关于聚合物中的横向交流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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