|
9 | 9 | .logLine { border-bottom:1px solid #ccc; padding:4px 2px; font-family:courier; font-size:11px; } |
10 | 10 | </style><script language="javascript"> |
11 | 11 | var uniqueId = 1; |
| 12 | + var testCallback; |
| 13 | + |
12 | 14 | function log(message, data) { |
13 | 15 | var log = document.getElementById('log') |
14 | 16 | var el = document.createElement('div') |
|
18 | 20 | else { log.appendChild(el) } |
19 | 21 | } |
20 | 22 | function setRightButtons() { |
21 | | - var buttons = [{"title":'按钮0'}, {"title":'按钮1'}]; |
22 | | - log('JS calling native method "setButtons"', buttons); |
23 | | - App.Page.callMethod('setButtons', buttons, function(index){ |
24 | | - var response = '点击了按钮' + index; |
25 | | - log('JS got native `setButtons` response', response); |
26 | | - }); |
| 23 | + var buttons = [{ "title": '按钮0' }, { "title": '按钮1' }]; |
| 24 | + log('JS calling native method "setButtons"', buttons); |
| 25 | + App.Page.callMethod('setButtons', buttons, function (index) { |
| 26 | + var response = '点击了按钮' + index; |
| 27 | + log('JS got native `setButtons` response', response); |
| 28 | + }); |
27 | 29 | } |
28 | 30 |
|
29 | 31 | function nslog(data) { |
30 | | - App.Bridge.callMethod('nslog', data, function(data) { |
31 | | - log('[1]JS got native `nslog` response', data); |
32 | | - }); |
| 32 | + App.Bridge.callMethod('nslog', data, function (data) { |
| 33 | + log('JS got native `nslog` response', data); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + function test(data) { |
| 38 | + if (testCallback) { |
| 39 | + log('calling callback', data); |
| 40 | + testCallback(data); |
| 41 | + } else { |
| 42 | + log('callback is undefined'); |
| 43 | + } |
33 | 44 | } |
| 45 | + |
| 46 | + App.Bridge.callMethod('testNativeMethod', { foo: 'foo', bar: 'bar' }, function (data) { |
| 47 | + log('JS got native `testNativeMethod` response', data); |
| 48 | + }); |
| 49 | + |
| 50 | + App.Bridge.registerMethod('log', function (data, callback) { |
| 51 | + var message = JSON.parse(data); |
| 52 | + log('Native calling js method `log`', message); |
| 53 | + callback({ key: 'from js', value: data }); |
| 54 | + }); |
34 | 55 |
|
35 | | - App.Bridge.callMethod('nslog', {bar: 'bar'}, function(data) { |
36 | | - log('[0]JS got native `nslog` response', data); |
37 | | - }); |
| 56 | + App.Bridge.registerMethod('test', function(data, callback) { |
| 57 | + log('save callback'); |
| 58 | + testCallback = callback; |
| 59 | + }, true); |
38 | 60 |
|
39 | | - App.Bridge.callMethod('testNativeMethod', {foo:'foo', bar: 'bar'}, function(data){ |
40 | | - log('JS got native `testNativeMethod` response', data); |
41 | | - }); |
42 | | - App.Bridge.registerMethod('log', function(data, callback){ |
43 | | - var message = JSON.parse(data); |
44 | | - log('Native calling js method `log`', message); |
45 | | - callback({key: 'from js', value: data}); |
46 | | - }); |
47 | 61 | </script> |
48 | 62 | </head><body> |
49 | 63 | <input type="button" value="Set Right Buttons" onclick="setRightButtons()" /> |
50 | 64 | <input type="button" value="nslog" onclick="nslog({'foo': 'foo'})" /> |
| 65 | + <input type="button" value="test callback" onclick="test({'key': 'value'})" /> |
51 | 66 | <div id='log'></div> |
52 | 67 | </body></html> |
0 commit comments