togoist/web/web.go

37 lines
748 B
Go
Raw Normal View History

2016-10-09 18:10:34 +00:00
package main
import (
"time"
"github.com/bep/debounce"
"github.com/gopherjs/jquery"
"gitlab.com/chrissexton/togoist"
)
var jQuery = jquery.NewJQuery
func convert(tmpl, json string, cb func(error, string)) {
go func() {
res, err := togoist.TogoString(tmpl, json)
cb(err, res)
}()
}
func handleChange() {
tmpl := jQuery("#tmpl").Val()
input := jQuery("#input").Val()
convert(tmpl, input, func(err error, out string) {
if err != nil {
jQuery("#output").SetText(err.Error())
return
}
jQuery("#output").SetText(out)
})
}
func main() {
debounced, _ := debounce.New(time.Second)
jQuery("#tmpl").On(jquery.KEYUP, func() { debounced(handleChange) })
jQuery("#input").On(jquery.KEYUP, func() { debounced(handleChange) })
}