OLD | NEW |
1 <!-- | 1 <!-- |
2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | 2 @license |
3 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE | 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS | 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
5 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS | 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
6 Code distributed by Google as part of the polymer project is also | 7 Code distributed by Google as part of the polymer project is also |
7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS | 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
8 --> | 9 --> |
9 <link rel="import" href="../polymer/polymer.html"> | 10 <link rel="import" href="../polymer/polymer.html"> |
10 | 11 |
11 <!-- | 12 <script> |
12 `core-signals` provides basic publish-subscribe functionality. | 13 (function(){ |
| 14 /** |
| 15 `iron-signals` provides basic publish-subscribe functionality. |
13 | 16 |
14 Note: avoid using `core-signals` whenever you can use | 17 Note: avoid using `iron-signals` whenever you can use |
15 a controller (parent element) to mediate communication | 18 a controller (parent element) to mediate communication |
16 instead. | 19 instead. |
17 | 20 |
18 To send a signal, fire a custom event of type `core-signal`, with | 21 To send a signal, fire a custom event of type `iron-signal`, with |
19 a detail object containing `name` and `data` fields. | 22 a detail object containing `name` and `data` fields. |
20 | 23 |
21 this.fire('core-signal', {name: 'hello', data: null}); | 24 this.fire('iron-signal', {name: 'hello', data: null}); |
22 | |
23 To receive a signal, listen for `core-signal-<name>` event on a | |
24 `core-signal` element. | |
25 | 25 |
26 <core-signal on-core-signal-hello="{{helloSignal}}"> | 26 To receive a signal, listen for `iron-signal-<name>` event on a |
| 27 `iron-signals` element. |
27 | 28 |
28 You can fire a signal event from anywhere, and all | 29 <iron-signals on-iron-signal-hello="{{helloSignal}}"> |
29 `core-signal` elements will receive the event, regardless | 30 |
| 31 You can fire a signal event from anywhere, and all |
| 32 `iron-signals` elements will receive the event, regardless |
30 of where they are in DOM. | 33 of where they are in DOM. |
31 | 34 |
32 @group Polymer Core Elements | 35 @demo demo/index.html |
33 @element core-signals | 36 */ |
34 @status stable | |
35 @homepage github.io | |
36 --> | |
37 <polymer-element name="core-signals"> | |
38 <script> | |
39 (function(){ | |
40 | |
41 Polymer({ | 37 Polymer({ |
| 38 is: 'iron-signals', |
| 39 |
42 attached: function() { | 40 attached: function() { |
43 signals.push(this); | 41 signals.push(this); |
44 }, | 42 }, |
45 removed: function() { | 43 detached: function() { |
46 var i = signals.indexOf(this); | 44 var i = signals.indexOf(this); |
47 if (i >= 0) { | 45 if (i >= 0) { |
48 signals.splice(i, 1); | 46 signals.splice(i, 1); |
49 } | 47 } |
50 } | 48 } |
51 }); | 49 }); |
52 | 50 |
53 // private shared database | 51 // private shared database |
54 var signals = []; | 52 var signals = []; |
55 | 53 |
56 // signal dispatcher | 54 // signal dispatcher |
57 function notify(name, data) { | 55 function notify(name, data) { |
58 // convert generic-signal event to named-signal event | 56 // convert generic-signal event to named-signal event |
59 var signal = new CustomEvent('core-signal-' + name, { | 57 var signal = new CustomEvent('iron-signal-' + name, { |
60 // if signals bubble, it's easy to get confusing duplicates | 58 // if signals bubble, it's easy to get confusing duplicates |
61 // (1) listen on a container on behalf of local child | 59 // (1) listen on a container on behalf of local child |
62 // (2) some deep child ignores the event and it bubbles | 60 // (2) some deep child ignores the event and it bubbles |
63 // up to said container | 61 // up to said container |
64 // (3) local child event bubbles up to container | 62 // (3) local child event bubbles up to container |
65 // also, for performance, we avoid signals flying up the | 63 // also, for performance, we avoid signals flying up the |
66 // tree from all over the place | 64 // tree from all over the place |
67 bubbles: false, | 65 bubbles: false, |
68 detail: data | 66 detail: data |
69 }); | 67 }); |
70 // dispatch named-signal to all 'signals' instances, | 68 // dispatch named-signal to all 'signals' instances, |
71 // only interested listeners will react | 69 // only interested listeners will react |
72 signals.forEach(function(s) { | 70 signals.forEach(function(s) { |
73 s.dispatchEvent(signal); | 71 s.dispatchEvent(signal); |
74 }); | 72 }); |
75 } | 73 } |
76 | 74 |
77 // signal listener at document | 75 // signal listener at document |
78 document.addEventListener('core-signal', function(e) { | 76 document.addEventListener('iron-signal', function(e) { |
79 notify(e.detail.name, e.detail.data); | 77 notify(e.detail.name, e.detail.data); |
80 }); | 78 }); |
81 | 79 |
82 })(); | 80 })(); |
83 </script> | 81 </script> |
84 </polymer-element> | |
OLD | NEW |