This commit is contained in:
alsotang 2014-09-07 15:08:56 +08:00
parent 127f060ca3
commit 2267b8c306
2 changed files with 19 additions and 8 deletions

View File

@ -211,15 +211,20 @@ exports.up = function (req, res, next) {
if (err) { if (err) {
return next(err); return next(err);
} }
var success = false; var action;
reply.ups = reply.ups || []; reply.ups = reply.ups || [];
if (reply.ups.indexOf(userId) === -1) { var upIndex = reply.ups.indexOf(userId);
if (upIndex === -1) {
reply.ups.push(userId); reply.ups.push(userId);
success = true; action = 'up';
} else {
reply.ups.splice(upIndex, 1);
action = 'down';
} }
reply.save(function () { reply.save(function () {
res.send({ res.send({
success: success success: true,
action: action,
}); });
}); });
}); });

View File

@ -328,10 +328,16 @@
method: 'POST', method: 'POST',
}).done(function (data) { }).done(function (data) {
if (data.success) { if (data.success) {
$this.next('.up-count').text((+$this.next('.up-count').text || 0) + 1); var currentCount = Number($this.next('.up-count').text().trim()) || 0;
$this.addClass('uped'); if (data.action === 'up') {
} else { $this.next('.up-count').text(currentCount + 1);
alert('重复投票。'); $this.addClass('uped');
} else {
if (data.action === 'down') {
$this.next('.up-count').text(currentCount - 1);
$this.removeClass('uped');
}
}
} }
}); });
}); });