zserge / lorca

Build cross-platform modern desktop apps in Go + HTML5
MIT License
8.04k stars 535 forks source link

Is there a vuejs SPA demo? #19

Closed JJJJJJJerk closed 6 years ago

JJJJJJJerk commented 6 years ago

how to send js object to golang in html code?

zserge commented 6 years ago

Bind a Go function with a structure as a parameter. Call that from JavaScript passing an object. Your Go function should be called with a structure parameter filled with object data.

Prinzhorn commented 4 years ago

If I understand correctly there is currently only an API to pull data from Go into JS, but none to push data from Go into JS, correct? The only way to push data from Go to JS is to use ui.Eval()? In the counter example the JS part triggers an increment and then pulls the counterValue, which tightly couples the JS and Go logic. In reality we might want to push data from Go to JS at any point, without JS "waiting" for it. Think of a "toast" message that we need to render in response to something that happened in the Go part. Right now I need to ui.Eval() and do some ugly string interpolation to call a JS function with a serialized JSON object. Or am I missing something?

Edit: Even though the counter example uses async/await it still assumes that Go is synchronous as far as I understand. The async/await is need, because the communication is asynchronous. If counterAdd were truly asynchronous (e.g. the value is incremented in a remote database), the example would break. Because render is called in JS and assumes that the next counterValue is updated. Which again, as I said above, tightly couples the rendering and logic components. What would be awesome would be a way for JS to observe changes in Go struct or something along those lines.