lateral communication in Polymer

佐手、 提交于 2019-12-10 10:15:04

问题


Here is the problem:

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.

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.

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


回答1:


You should be able to do this using polymer-signals

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

Quoting from the doc:

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

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

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>


来源:https://stackoverflow.com/questions/24241534/lateral-communication-in-polymer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!