Ejemplo n.º 1
0
func (v *NewPerson) CreatePerson(ev dom.Event) {
	ev.PreventDefault()
	formEl, ok := ev.CurrentTarget().(*dom.HTMLFormElement)
	if !ok {
		panic("Could not cast target to dom.HTMLFormElement: " + fmt.Sprintf("%T", ev.CurrentTarget()))
	}
	f, err := form.Parse(formEl)
	if err != nil {
		panic(err)
	}
	f.Validate("name").Required()
	f.Validate("age").Required().IsInt().Greater(0)
	if f.HasErrors() {
		if errorsView == nil {
			errorsView = NewErrors(f.Errors)
			view.InsertBefore(errorsView, v)
		}
		errorsView.Errors = f.Errors
		if err := errorsView.Render(); err != nil {
			panic(err)
		}
		return
	}
	person := &models.Person{}
	if err := f.Bind(person); err != nil {
		panic(err)
	}
	restClient := rest.NewClient()
	restClient.ContentType = rest.ContentJSON
	go func() {
		if err := restClient.Create(person); err != nil {
			if httpErr, ok := err.(rest.HTTPError); ok {
				fmt.Println(string(httpErr.Body))
			}
			panic(err)
		}
		v.Router.Navigate("/people")
	}()
}
Ejemplo n.º 2
0
// UpdateRepository updates specified repository.
// repoRoot is the import path corresponding to the root of the repository.
func UpdateRepository(event dom.Event, repoRoot string) {
	event.PreventDefault()
	if event.(*dom.MouseEvent).Button != 0 {
		return
	}

	repoUpdate := document.GetElementByID(repoRoot)
	updateButton := repoUpdate.GetElementsByClassName("update-button")[0].(*dom.HTMLAnchorElement)

	updateButton.SetTextContent("Updating...")
	updateButton.AddEventListener("click", false, func(event dom.Event) { event.PreventDefault() })
	updateButton.SetTabIndex(-1)
	updateButton.Class().Add("disabled")

	go func() {
		req := xhr.NewRequest("POST", "/-/update")
		req.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
		err := req.Send(url.Values{"repo_root": {repoRoot}}.Encode())
		if err != nil {
			println(err.Error())
			return
		}

		// Hide the "Updating..." label.
		updateButton.Style().SetProperty("display", "none", "")

		// Show "No Updates Available" if there are no remaining updates.
		if !anyUpdatesRemaining() {
			document.GetElementByID("no_updates").(dom.HTMLElement).Style().SetProperty("display", "", "")
		}

		// Move this Go package to "Installed Updates" list.
		installedUpdates := document.GetElementByID("installed_updates").(dom.HTMLElement)
		installedUpdates.Style().SetProperty("display", "", "")
		installedUpdates.ParentNode().InsertBefore(repoUpdate, installedUpdates.NextSibling()) // Insert after.
	}()
}