-
Notifications
You must be signed in to change notification settings - Fork 570
js struct tag - how does it work? #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Your struct needs to have a field with the type |
Doesn't appear to help:
Yields:
and (in the embedded case):
|
I tried removing |
I guess what you want is something like this: package main
import "github.com/gopherjs/gopherjs/js"
type Class struct {
js.Object
DisplayName string `js:"displayName"`
Render func() `js:"render"`
}
func main() {
c := Class{Object: js.Global.Get("Object").New()}
c.DisplayName = "Player"
c.Render = func() {
println("Render called!")
}
println(c.Object)
} Output:
The js tags are still in a rough state. Please feel free to share any ideas that you have on them. |
When I do that (use the
I need to pass a javascript object with displayName and render to an external javascript library I don't control. Is there a better way to do this? [Update:] I tried your code and it works fine, so I must be doing something wrong. I'll research why mine isn't working how it looks like it should and post a new comment here. |
It appears that one must not set other struct members other than the js.Object at var init time, but instead set them as individual statements. This is strange, and it's not obvious why one needs js.Object at all for this to happen. |
The type Class struct {
js.Object
}
func (c *Class) DisplayName() string {
return c.Object.Get("displayName").Str()
}
func (c *Class) SetDisplayName(displayName string) {
c.Object.Set("displayName", displayName)
} But in a shorter form: type Class struct {
js.Object
DisplayName string `js:"displayName"`
} Basically, getting or setting a struct field with a |
@shurcooL Exactly, that's a very good explanation. |
Is this means that the But |
Hi @Archs , please include a link to an example of your issue on http://www.gopherjs.org/play/ . |
Hi @neelance, I'm working on a gopherjs binding of Vue.js, a MVVM js library, which is quite like Angular.js but different. Vuejs's initialization method takes a So I tried using a struct literal to pass the configs like this: type VueOption struct {
js.Object
El string `js:"el"`
Data js.M `js:"data"`
Methods js.M `js:"methods"`
...
}
// init func
func New(opts VueOption ) *Vue {
vm := vue.New(opts)
return &Vue{
Object: vm,
}
}
// then initializing a vuejs instance
v := vue.New(vue.VueOption{
El: "#demo",
Data: js.M{
"title": "todos",
"todos": []js.M{
js.M{
"done": true,
"content": "Learn JavaScript",
},
js.M{
"done": false,
"content": "Learn Vue.js",
},
},
},
}) but it won't work. I have to use the // init func
func New(opts js.M) *Vue {
vm := vue.New(opts)
return &Vue{
Object: vm,
}
}
// then initializing a vuejs instance
v := vue.New(js.M{
"el": "#demo",
"data": js.M{
"title": "todos",
"todos": []js.M{
js.M{
"done": true,
"content": "Learn JavaScript",
},
js.M{
"done": false,
"content": "Learn Vue.js",
},
},
},
}) The I think this relates to issue:#141 since the js tags in Go struct only serve as a setter/getter shortcuts, without direct access the struct fields can't be recognized by the Js world. Is there some way to work around this? |
Hi, all. On Mon, Feb 2, 2015 at 9:42 AM, Archs notifications@github.com wrote:
Best regards, |
I'm working on reactjs bindings, but can't figure out how the struct tags work. I have:
Printing
c
in the js console, I get:I expected it to have members
displayName
andrender
due to the struct tags. Am I missing something?The text was updated successfully, but these errors were encountered: