Chromium Code Reviews| Index: native_client_sdk/src/examples/api/messaging/example.js |
| diff --git a/native_client_sdk/src/examples/api/messaging/example.js b/native_client_sdk/src/examples/api/messaging/example.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4c5a6828cea023b76edbf6cf938324179d60157e |
| --- /dev/null |
| +++ b/native_client_sdk/src/examples/api/messaging/example.js |
| @@ -0,0 +1,39 @@ |
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +function $(id) { |
| + return document.querySelector(id); |
| +} |
|
dmichael (off chromium)
2014/12/03 23:33:33
I wouldn't define $ in an example... you can just
binji
2014/12/03 23:56:50
Done.
|
| + |
| +function attachListeners() { |
| + var number1El = $('#addend1'); |
| + var number2El = $('#addend2'); |
| + var resultEl = $('#result'); |
| + |
| + document.getElementById('addAsync').addEventListener('click', function() { |
| + var value1 = parseInt(number1El.value); |
| + var value2 = parseInt(number2El.value); |
| + common.naclModule.postMessage([value1, value2]); |
| + |
| + // The result is returned in handleMessage below. |
| + }); |
| + |
| + document.getElementById('addSync').addEventListener('click', function() { |
| + var value1 = parseInt(number1El.value); |
| + var value2 = parseInt(number2El.value); |
| + var result = |
| + common.naclModule.postMessageAndAwaitResponse([value1, value2]); |
| + |
| + // This is the result returned from the module synchronously (i.e. when the |
| + // addSync button is pressed) |
| + resultEl.textContent = result; |
| + }); |
| +} |
| + |
| +// Called by the common.js module. |
| +function handleMessage(message_event) { |
| + // This is the result returned from the module asynchronously (i.e. when the |
| + // addAsync button is pressed) |
| + result.textContent = message_event.data; |
| +} |