Skip to content

Instantly share code, notes, and snippets.

@luna-duclos
Created December 17, 2015 17:57
Show Gist options
  • Save luna-duclos/0f401fc7caa928b9fbb6 to your computer and use it in GitHub Desktop.
Save luna-duclos/0f401fc7caa928b9fbb6 to your computer and use it in GitHub Desktop.
func Register(proto Interface) {
// Type detection
refType := reflect.TypeOf(proto)
if refType.Kind() == reflect.Ptr {
refType = refType.Elem()
}
// Setup js.M object
m := js.M{}
m["is"] = proto.TagName()
m["extends"] = proto.Extends()
m["created"] = createdCallback(refType)
// Register our prototype with polymer
js.Global.Call("Polymer", m)
}
func createdCallback(refType reflect.Type) *js.Object {
return js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
// Create a new Go side object and keep it around in this closure
// That way, we can keep track of it across callbacks and calls
newVal := reflect.New(refType).Interface().(Interface)
// Call Created on the newly initialized struct
fmt.Println("Calling created callback")
newVal.Created()
// Setup other callbacks
this.Set("ready", func() {
fmt.Println("Calling ready callback")
newVal.Ready()
})
this.Set("attached", func() {
fmt.Println("Calling attached callback")
newVal.Attached()
})
this.Set("detached", func() {
fmt.Println("Calling detached callback")
newVal.Detached()
})
return nil
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment