5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-10 05:02:36 +08:00

[v3 examples/bindings] correct binding output + example

- disable output of `import <model> from models` line
- update README to generate bindings in usable location for HTML
- update HTML to reference bindings correctly
- update javascript to call and process the bound Greet function
This commit is contained in:
Travis McLane 2023-10-31 17:24:00 -05:00
parent 144567410d
commit c6ecbd56e5
7 changed files with 35 additions and 35 deletions

View File

@ -2,10 +2,10 @@
This example demonstrates how to generate bindings for your application. This example demonstrates how to generate bindings for your application.
To generate bindings, run `wails3 generate bindings` in this directory. To generate bindings, run `wails3 generate bindings -d assets` in this directory.
See more options by running `wails3 generate bindings --help`. See more options by running `wails3 generate bindings --help`.
## Notes ## Notes
- The bindings generator is still a work in progress and is subject to change. - The bindings generator is still a work in progress and is subject to change.
- The generated code uses `wails.CallByID` by default. This is the most secure and quickest way to call a function. In a future release, we will look at generating `wails.CallByName` too. - The generated code uses `wails.CallByID` by default. This is the most secure and quickest way to call a function. In a future release, we will look at generating `wails.CallByName` too.

View File

@ -2,7 +2,6 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
import {main} from './models';
window.go = window.go || {}; window.go = window.go || {};
window.go.main = { window.go.main = {
@ -14,7 +13,7 @@ window.go.main = {
* @param name {string} * @param name {string}
* @returns {Promise<string>} * @returns {Promise<string>}
**/ **/
Greet: function(name) { wails.CallByID(1411160069, ...Array.prototype.slice.call(arguments, 0)); }, Greet: function(name) { return wails.CallByID(1411160069, ...Array.prototype.slice.call(arguments, 0)); },
/** /**
* GreetService.GreetPerson * GreetService.GreetPerson
@ -22,7 +21,6 @@ window.go.main = {
* @param person {main.Person} * @param person {main.Person}
* @returns {Promise<string>} * @returns {Promise<string>}
**/ **/
GreetPerson: function(person) { wails.CallByID(4021313248, ...Array.prototype.slice.call(arguments, 0)); }, GreetPerson: function(person) { return wails.CallByID(4021313248, ...Array.prototype.slice.call(arguments, 0)); },
}, },
}; };

View File

@ -75,20 +75,20 @@
<p class="button-42" onclick="window.open('https://wailsapp.github.io/wails/')">Documentation</p> <p class="button-42" onclick="window.open('https://wailsapp.github.io/wails/')">Documentation</p>
<p class="button-42" onclick="window.open('https://wailsapp.github.io/wails/getting-started/feedback/')">Feedback</p> <p class="button-42" onclick="window.open('https://wailsapp.github.io/wails/getting-started/feedback/')">Feedback</p>
</div> </div>
<div class="result" id="result">{resultText}</div> <div class="result" id="result">Please enter your name below 👇</div>
<div class="input-box" id="input"> <div class="input-box" id="input">
<input autocomplete="off" class="input" id="name" type="text"/> <input autocomplete="off" class="input" id="name" type="text"/>
<button class="btn" onclick="greet">Greet</button> <button class="btn" onclick="greet()">Greet</button>
</div> </div>
<script src='bindings_main.js'></script>
<script> <script>
import {Greet} from '../assets/bindings_main.js'; let resultText = "";
let name = "";
let resultText = "Please enter your name below 👇" function greet() {
let name window.go.main.GreetService.Greet(document.getElementById("name").value).then((result) =>
document.getElementById("result").innerHTML = result);
function greet() { }
Greet(name).then(result => resultText = result)
}
</script> </script>
</body> </body>
</html> </html>

View File

@ -2,7 +2,6 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
import {main} from './models';
window.go = window.go || {}; window.go = window.go || {};
window.go.main = { window.go.main = {
@ -14,7 +13,7 @@ window.go.main = {
* @param name {string} * @param name {string}
* @returns {Promise<string>} * @returns {Promise<string>}
**/ **/
Greet: function(name) { wails.CallByID(1411160069, ...Array.prototype.slice.call(arguments, 0)); }, Greet: function(name) { return wails.CallByID(1411160069, ...Array.prototype.slice.call(arguments, 0)); },
/** /**
* GreetService.GreetPerson * GreetService.GreetPerson
@ -22,7 +21,6 @@ window.go.main = {
* @param person {main.Person} * @param person {main.Person}
* @returns {Promise<string>} * @returns {Promise<string>}
**/ **/
GreetPerson: function(person) { wails.CallByID(4021313248, ...Array.prototype.slice.call(arguments, 0)); }, GreetPerson: function(person) { return wails.CallByID(4021313248, ...Array.prototype.slice.call(arguments, 0)); },
}, },
}; };

View File

@ -15,6 +15,7 @@ require (
github.com/go-git/go-billy/v5 v5.2.0 // indirect github.com/go-git/go-billy/v5 v5.2.0 // indirect
github.com/go-git/go-git/v5 v5.3.0 // indirect github.com/go-git/go-git/v5 v5.3.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-ole/go-ole v1.2.6 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect github.com/imdario/mergo v0.3.12 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect

View File

@ -30,6 +30,8 @@ github.com/go-git/go-git/v5 v5.3.0 h1:8WKMtJR2j8RntEXR/uvTKagfEt4GYlwQ7mntE4+0GW
github.com/go-git/go-git/v5 v5.3.0/go.mod h1:xdX4bWJ48aOrdhnl2XqHYstHbbp6+LFS4r4X+lNVprw= github.com/go-git/go-git/v5 v5.3.0/go.mod h1:xdX4bWJ48aOrdhnl2XqHYstHbbp6+LFS4r4X+lNVprw=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
@ -105,8 +107,7 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tmclane/purego v0.0.0-20230601213035-1f25e70d7b01 h1:oQwu3iNDywGp1Hry+PDvz+grwbCGpzY+ckSnWKCnX5Y= github.com/tmclane/purego v0.0.0-20230601213035-1f25e70d7b01 h1:oQwu3iNDywGp1Hry+PDvz+grwbCGpzY+ckSnWKCnX5Y=
github.com/tmclane/purego v0.0.0-20230601213035-1f25e70d7b01/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= github.com/tmclane/purego v0.0.0-20230601213035-1f25e70d7b01/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/wailsapp/go-webview2 v1.0.8 h1:hyoFPlMSfb/NM64wuVbgBaq1MASJjqsSUYhN+Rbcr9Y= github.com/wailsapp/go-webview2 v1.0.9 h1:lrU+q0cf1wgLdR69rN+ZnRtMJNaJRrcQ4ELxoO7/xjs=
github.com/wailsapp/go-webview2 v1.0.8/go.mod h1:Uk2BePfCRzttBBjFrBmqKGJd41P6QIHeV9kTgIeOZNo=
github.com/wailsapp/go-webview2 v1.0.9/go.mod h1:Uk2BePfCRzttBBjFrBmqKGJd41P6QIHeV9kTgIeOZNo= github.com/wailsapp/go-webview2 v1.0.9/go.mod h1:Uk2BePfCRzttBBjFrBmqKGJd41P6QIHeV9kTgIeOZNo=
github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhwHs= github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhwHs=
github.com/wailsapp/mimetype v1.4.1/go.mod h1:9aV5k31bBOv5z6u+QP8TltzvNGJPmNJD4XlAL3U+j3o= github.com/wailsapp/mimetype v1.4.1/go.mod h1:9aV5k31bBOv5z6u+QP8TltzvNGJPmNJD4XlAL3U+j3o=
@ -154,7 +155,8 @@ gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -22,7 +22,7 @@ const bindingTemplate = `
* @param name {string} * @param name {string}
* @returns {Promise<string>} * @returns {Promise<string>}
**/ **/
{{methodName}}: function({{inputs}}) { wails.CallByID({{ID}}, ...Array.prototype.slice.call(arguments, 0)); }, {{methodName}}: function({{inputs}}) { return wails.CallByID({{ID}}, ...Array.prototype.slice.call(arguments, 0)); },
` `
var reservedWords = []string{ var reservedWords = []string{
@ -248,17 +248,18 @@ window.go = window.go || {};
result[normalisedPackageNames[packageName]] += "};\n" result[normalisedPackageNames[packageName]] += "};\n"
// add imports // add imports
if len(allModels) > 0 { /* if len(allModels) > 0 {
allModels := lo.Uniq(allModels) allModels := lo.Uniq(allModels)
var models []string var models []string
for _, model := range allModels { for _, model := range allModels {
models = append(models, normalisedPackageNames[model]) models = append(models, normalisedPackageNames[model])
} }
sort.Strings(models) sort.Strings(models)
result[normalisedPackageNames[packageName]] += "\n" result[normalisedPackageNames[packageName]] += "\n"
imports := "import {" + strings.Join(models, ", ") + "} from './models';\n" imports := "import {" + strings.Join(models, ", ") + "} from './models';\n"
result[normalisedPackageNames[packageName]] = imports + result[normalisedPackageNames[packageName]] result[normalisedPackageNames[packageName]] = imports + result[normalisedPackageNames[packageName]]
} }
*/
result[normalisedPackageNames[packageName]] = header + result[normalisedPackageNames[packageName]] result[normalisedPackageNames[packageName]] = header + result[normalisedPackageNames[packageName]]
} }