You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Wondering how one can pass a struct into Call and have it produce a dictionary with lowercase keys rather than uppercase exported keys.
package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/gopherjs/jquery"
)
var jQ = jquery.NewJQuery
type A struct {
C string
}
func main() {
jQ().Ready(func() {
a := A{C: "test"}
js.Global.Call("alert", a)
})
}
When evaluating in the console this gives:
a
Object { $val: Object, C: "test" }
$externalize(a, A)
Object { C: "test" }
I want Object { c: "test" }. No doubt I am missing something obvious.
The text was updated successfully, but these errors were encountered:
I don't think there is currently a way of doing this without using a struct with a special *js.Object field in position zero; see the gotchas wiki for more details.
package main
import (
"github.com/gopherjs/gopherjs/js"
)
typeS1struct {
C1string`js:"c1"`// tag has no effect
}
typeS2struct {
*js.ObjectC1string`js:"c1"`// tag has effect because of *js.Object at position 0C2string
}
funcmain() {
s1:=S1{C1: "S1 C1"}
// output will be: Object {C1: "S1 C1"}js.Global.Get("console").Call("log", "s1", s1)
// s2 must be a *S2; see the gotchas wikis2:=&S2{Object: js.Global.Get("Object").New()}
// this js-tagged field must be set separate from the initialization// of s2; again, see the gotchas wikis2.C1="S2 C1"s2.C2="S2 C2"// output will be: Object {c1: "S2 C1"}js.Global.Get("console").Call("log", "s2", s2)
}
But in converting your type to have a *js.Object field in position zero, beware of the gotcha that starts "In structs with *js.Object fields, the object exists in two parts..."
I'm looking to try and improve the gotchas wiki to explain this better, but I also think the use of js: tags could be made more general as you're alluding to here.
Wondering how one can pass a struct into Call and have it produce a dictionary with lowercase keys rather than uppercase exported keys.
When evaluating in the console this gives:
I want Object { c: "test" }. No doubt I am missing something obvious.
The text was updated successfully, but these errors were encountered: