Skip to content

Commit 30628f6

Browse files
committed
feat: update examples to use new webui function api
1 parent 7d4f4c2 commit 30628f6

1 file changed

Lines changed: 29 additions & 33 deletions

File tree

examples/call_v_from_js.v

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,70 +22,66 @@ const doc = '<!DOCTYPE html>
2222
<h1>WebUI - Call V from JavaScript</h1>
2323
<br>
2424
<p>Call V functions with arguments (<em>See the logs in your terminal</em>)</p>
25-
<button onclick="webui.call(\'MyID_One\', \'Hello\', \'World\');">Call my_function_string()</button>
25+
<button onclick="webui.handleStr(\'Hello\', \'World\');">Call handle_str()</button>
2626
<br>
27-
<button onclick="webui.call(\'MyID_Two\', 123, 456, 789);">Call my_function_integer()</button>
27+
<button onclick="webui.handleInt(123, 456, 789);">Call handle_int()</button>
2828
<br>
29-
<button onclick="webui.call(\'MyID_Three\', true, false);">Call my_function_boolean()</button>
29+
<button onclick="webui.handleBool(true, false);">Call handle_bool()</button>
3030
<br>
3131
<p>Call a V function that returns a response</p>
32-
<button onclick="MyJS();">Call my_function_with_response()</button>
33-
<div>Double: <input type="text" id="MyInputID" value="2"></div>
32+
<button onclick="MyJS();">Call get_response()</button>
33+
<div>Double: <input type="text" id="my-input" value="2"></div>
3434
<script>
3535
async function MyJS() {
36-
const MyInput = document.getElementById("MyInputID");
37-
const number = MyInput.value;
38-
const result = await webui.call("MyID_Four", number);
39-
MyInput.value = result;
36+
const myInput = document.getElementById("my-input");
37+
const number = myInput.value;
38+
const result = await webui.getResponse(number);
39+
myInput.value = result;
4040
}
4141
</script>
4242
</body>
4343
</html>'
4444

45-
// JavaScript:
46-
// webui.call('MyID_One', 'Hello');
47-
fn my_function_string(e &ui.Event) voidptr {
45+
// JavaScript: `webui.handleStr('Hello', 'World');`
46+
fn handle_str(e &ui.Event) voidptr {
4847
str1 := e.get_arg[string]() or { return ui.no_result }
4948
str2 := e.get_arg[string](idx: 1) or { '' }
5049

51-
println('my_function_string 1: ${str1}') // Hello
52-
println('my_function_string 2: ${str2}') // World
50+
println('handle_str 1: ${str1}') // Hello
51+
println('handle_str 2: ${str2}') // World
5352

5453
return ui.no_result
5554
}
5655

57-
// JavaScript:
58-
// webui.call('MyID_Two', 123456789);
59-
fn my_function_integer(e &ui.Event) voidptr {
56+
// JavaScript: `webui.handleInt(123, 456, 789);`
57+
fn handle_int(e &ui.Event) voidptr {
6058
num1 := e.get_arg[int](idx: 0) or { return ui.no_result }
6159
num2 := e.get_arg[int](idx: 1) or { 0 }
6260
num3 := e.get_arg[int](idx: 2) or { 0 }
6361

64-
println('my_function_integer 1: ${num1}') // 123
65-
println('my_function_integer 2: ${num2}') // 456
66-
println('my_function_integer 3: ${num3}') // 789
62+
println('handle_int 1: ${num1}') // 123
63+
println('handle_int 2: ${num2}') // 456
64+
println('handle_int 3: ${num3}') // 789
6765

6866
return ui.no_result
6967
}
7068

71-
// JavaScript:
72-
// webui.call('MyID_Three', true);
73-
fn my_function_boolean(e &ui.Event) voidptr {
69+
// JavaScript: webui.handleBool(true, false);
70+
fn handle_bool(e &ui.Event) voidptr {
7471
status1 := e.get_arg[bool]() or { return ui.no_result }
7572
status2 := e.get_arg[bool](idx: 1) or { return ui.no_result }
7673

77-
println('my_function_boolean 1: ${status1}') // true
78-
println('my_function_boolean 2: ${status2}') // false
74+
println('handle_bool 1: ${status1}') // true
75+
println('handle_bool 2: ${status2}') // false
7976

8077
return ui.no_result
8178
}
8279

83-
// JavaScript:
84-
// const result = webui.call('MyID_Four', number);
85-
fn my_function_with_response(e &ui.Event) int {
80+
// JavaScript: `const result = await webui.getResponse(number);`
81+
fn get_response(e &ui.Event) int {
8682
number := e.get_arg[int]() or { return 0 } * 2
8783

88-
println('my_function_with_response: ${number}')
84+
println('get_response: ${number}')
8985

9086
return number
9187
}
@@ -96,10 +92,10 @@ mut w := ui.new_window()
9692
// Show the window, panic on fail.
9793
w.show(doc)!
9894

99-
w.bind('MyID_One', my_function_string)
100-
w.bind('MyID_Two', my_function_integer)
101-
w.bind('MyID_Three', my_function_boolean)
102-
w.bind('MyID_Four', my_function_with_response)
95+
w.bind('handleStr', handle_str)
96+
w.bind('handleInt', handle_int)
97+
w.bind('handleBool', handle_bool)
98+
w.bind('getResponse', get_response)
10399

104100
// Wait until all windows get closed.
105101
ui.wait()

0 commit comments

Comments
 (0)