|
1 | 1 | # postRobot
|
2 | 2 |
|
3 |
| -Simple postMessage based server. Good for if you want to use post-messaging between windows, but you don't like the |
4 |
| -fire-and-forget nature of post messages. |
| 3 | +Simple postMessage based server. |
5 | 4 |
|
6 |
| -With this, you can set up a listener in one window, have it wait for a post message, and then have it reply with data. |
| 5 | +Use this if you want to communicate between two different windows (or popups, or iframes) using `window.postMessage`, |
| 6 | +but you don't like the fire-and-forget nature of the native api. |
| 7 | + |
| 8 | +With this, you can set up a listener in one window, have it wait for a post message, and then have it reply with data, |
| 9 | +and gracefully handle any errors that crop up. You can also set a timeout, to be sure that the other window is responding to you, |
| 10 | +and fail gracefully if it does not. |
7 | 11 |
|
8 | 12 | ## Example
|
9 | 13 |
|
10 |
| - // In one window |
| 14 | +```javascript |
11 | 15 |
|
12 |
| - postRobot.listen({ |
| 16 | +// In one window |
13 | 17 |
|
14 |
| - name: 'get_cart', |
| 18 | +postRobot.listen({ |
15 | 19 |
|
16 |
| - handler: function(data, callback) { |
17 |
| - setTimeout(function() { |
18 |
| - return callback(null, { |
19 |
| - amount: 1, |
20 |
| - shipping: 2 |
21 |
| - }); |
22 |
| - }, 3000) |
23 |
| - } |
24 |
| - }); |
| 20 | + name: 'get_cart', |
| 21 | + |
| 22 | + handler: function(data, callback) { |
| 23 | + setTimeout(function() { |
| 24 | + return callback(null, { |
| 25 | + amount: 1, |
| 26 | + shipping: 2 |
| 27 | + }); |
| 28 | + }, 3000) |
| 29 | + } |
| 30 | +}); |
25 | 31 |
|
26 |
| - // In another window |
| 32 | +// In another window |
27 | 33 |
|
28 |
| - postRobot.request({ |
| 34 | +postRobot.request({ |
29 | 35 |
|
30 |
| - window: window, |
31 |
| - name: 'get_cart', |
| 36 | + window: window, |
| 37 | + name: 'get_cart', |
| 38 | + |
| 39 | + response: function(err, data) { |
| 40 | + if (err) { |
| 41 | + throw err; |
| 42 | + } |
| 43 | + console.log('got cart!', data) |
| 44 | + }, |
32 | 45 |
|
33 |
| - response: function(err, data) { |
34 |
| - if (err) { |
35 |
| - throw err; |
36 |
| - } |
37 |
| - console.log('got cart!', data) |
38 |
| - }, |
| 46 | + timeout: 1000 |
| 47 | +}); |
39 | 48 |
|
40 |
| - timeout: 1000 |
41 |
| - }); |
| 49 | +``` |
0 commit comments