|
1 | | -// const trame = new Trame({ iframe }); |
2 | | -// await trame.connect({ application: 'trame' }); |
| 1 | +# Trame iframe client library for plain JS |
| 2 | +This library aims to simplify interaction between a trame application living inside an iframe and its iframe parent. |
| 3 | +This work is inspired by the [official trame-client js lib](https://github.com/Kitware/trame-client/tree/master/js-lib) |
3 | 4 |
|
4 | | -// State handing |
5 | | -trame.state.set("a", 5); |
6 | | -console.log(trame.state.get("b")); |
7 | | -trame.state.update({ |
8 | | - a: 1, |
9 | | - b: 2, |
10 | | -}); |
| 5 | +## Examples |
| 6 | +- [Vite](./examples/vite/) |
| 7 | + |
| 8 | +## Usage |
| 9 | +First you need to grab the iframe that contains your trame application. |
| 10 | +```js |
| 11 | +import ClientCommunicator from "@kitware/trame-iframe-client"; |
| 12 | + |
| 13 | +const iframe = document.getElementById("trame_app"); |
| 14 | +const iframe_url = "http://localhost:3000"; |
11 | 15 |
|
12 | | -// Method call on Python |
13 | | -const result = await trame.trigger("name", [arg_0, arg_1], { kwarg_0: 1, kwarg_1: 2 }); |
| 16 | +const trame = new ClientCommunicator(iframe, iframe_url); |
14 | 17 |
|
15 | | -// TODO - state watching |
16 | | -trame.state.watch(["a"], (a) => { |
17 | | - console.log(`a changed to ${a}`); |
| 18 | +// set |
| 19 | +trame.state.set("a", 2); |
| 20 | +trame.state.set('b', 3); |
| 21 | +trame.state.update({ |
| 22 | + a: 2.5, |
| 23 | + b: 3.5, |
| 24 | + c: 4.5, |
18 | 25 | }) |
| 26 | + |
| 27 | +// get |
| 28 | +console.log(trame.state.get("c")); |
| 29 | +console.log(trame.state.get('a')); |
| 30 | + |
| 31 | + |
| 32 | +// simple api for state change |
| 33 | +trame.state.watch( |
| 34 | + ["a", "b", "c"], |
| 35 | + (a, b, c) => { |
| 36 | + console.log(`a(${a}) or b(${b}) or c(${c}) have changed`); |
| 37 | + } |
| 38 | +); |
| 39 | + |
| 40 | +// ----------------------------------- |
| 41 | +// Method execution API |
| 42 | +// ----------------------------------- |
| 43 | + |
| 44 | +// method execution on Python side |
| 45 | +trame.trigger("name", ['arg_0', 'arg_1'], { kwarg_0: 1, kwarg_1: 2 }); |
| 46 | +``` |
0 commit comments