Esempio n. 1
0
// DoVote performs the vote with the given point changed (-1 = vote down, 1 = vote up)
func (m *VoteBoxModel) DoVote(vote int) {
	if m.VoteUrl == "" {
		panic("VoteUrl has not been set.")
	}

	if vote < -1 || vote > 1 {
		panic("Illegal vote value.")
	}

	lastVote := m.Vote.Voted
	m.Vote.Voted = vote
	if vote == lastVote {
		vote, lastVote = -lastVote, 0
		m.Vote.Voted = 0
	}

	url := utils.UrlQuery(m.VoteUrl, utils.Map{
		"vote":     vote,
		"lastvote": lastVote,
	})

	// performs an http request to the server to vote, and assign the updated score
	// to m.Vote.Score after that
	go func() {
		r, _ := app().Http.GET(url)
		r.ParseJSON(&m.Vote.Score)

		if m.AfterVote != nil {
			m.AfterVote()
		}

		app().Render()
	}()
}
Esempio n. 2
0
func (vm *PostsPageVM) Request(rankMode string) {
	if vm.RankMode == rankMode {
		return
	}

	vm.RankMode = rankMode
	url := utils.UrlQuery("/api/posts", utils.Map{"sort": vm.RankMode})
	r, _ := vm.httpClient.GET(url)
	err := r.ParseJSON(&vm.Posts)

	if err != nil {
		panic(err)
	}
}
Esempio n. 3
0
func (vm *CommentsPageVM) Request(rankMode string) {
	if vm.RankMode == rankMode {
		return
	}

	vm.RankMode = rankMode

	route := fmt.Sprintf("/api/comments/%v", vm.Post.Id)
	url := utils.UrlQuery(route, utils.Map{"sort": vm.RankMode})
	r, _ := vm.httpClient.GET(url)
	err := r.ParseJSON(&vm.Comments)
	if err != nil {
		panic(err)
	}
}