Skip to content

Commit

Permalink
add example of backend functions called in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
bmw2621 committed Jul 28, 2021
1 parent 71e8f60 commit 2ef6d3e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
9 changes: 4 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"log"

"github.com/zserge/lorca"
Expand All @@ -20,12 +19,12 @@ func main() {
ui.Load(addr)

// Call JS function from Go. Functions may be asynchronous, i.e. return promises
n := ui.Eval(`Math.random()`).Float()
fmt.Println(n)
// n := ui.Eval(`Math.random()`).Float()
// fmt.Println(n)

// Call JS that calls Go and so on and so on...
m := ui.Eval(`add(2, 3)`).Int()
fmt.Println(m)
// m := ui.Eval(`add(2, 3)`).Int()
// fmt.Println(m)

// Wait for the browser window to be closed
<-ui.Done()
Expand Down
22 changes: 22 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
<script>
export let name;
let a;
let b;
let c;
const handleEqualButtonClick = async () => {
c = await window.add(a, b)
}
</script>

<main>
<h1>Hello {name}!</h1>
<p>Visit the <a href="https://svelte.dev/tutorial">Svelte tutorial</a> to learn how to build Svelte apps.</p>
<div id="add">
<input type="number" bind:value={a}/> + <input type="number" bind:value={b}>
</div>
<button on:click={handleEqualButtonClick}>Equals</button>
{#if c}
<h1>{c}</h1>
{/if}
</main>

<style>
#add{
display: flex;
justify-content: center;
align-items: center;
}
main {
text-align: center;
padding: 1em;
Expand Down

0 comments on commit 2ef6d3e

Please sign in to comment.