func (v *HTMLView) addEventListeners(items Items, form js.Object) { inputDate := form.Call("querySelector", "input[name=Date]") inputDate.Set("onchange", func(e js.Object) { id, err := getIDFromElement(e.Get("target")) if err != nil { v.onErrorFunc(err) return } dateStr := e.Get("target").Get("value").Str() d, err := date.ParseISO8601(dateStr) if err != nil { v.onErrorFunc(err) return } if err := items.UpdateDate(id, d); err != nil { v.onErrorFunc(err) return } }) inputSubject := form.Call("querySelector", "input[name=Subject]") inputSubject.Set("onchange", func(e js.Object) { id, err := getIDFromElement(e.Get("target")) if err != nil { v.onErrorFunc(err) return } subject := e.Get("target").Get("value").Str() if err := items.UpdateSubject(id, subject); err != nil { v.onErrorFunc(err) return } }) inputMoneyAmount := form.Call("querySelector", "input[name=Amount]") inputMoneyAmount.Set("onchange", func(e js.Object) { id, err := getIDFromElement(e.Get("target")) if err != nil { v.onErrorFunc(err) return } amount := int32(e.Get("target").Get("value").Int()) if err := items.UpdateAmount(id, amount); err != nil { v.onErrorFunc(err) return } }) }
func (v *HTMLView) OnHashChange(e js.Object) { hash := js.Global.Get("location").Get("hash").Str() // Remove the initial '#' if 1 <= len(hash) { hash = hash[1:] } switch hash { case "": removeSingleHash() v.updateMode(items.ModeTop, date.Date(0)) default: ym, err := date.ParseISO8601(hash + "-01") if err != nil { v.onErrorFunc(err) return } v.updateMode(items.ModeYearMonth, ym) } }