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) {
return next(err);
}
var success = false;
var action;
reply.ups = reply.ups || [];
if (reply.ups.indexOf(userId) === -1) {
var upIndex = reply.ups.indexOf(userId);
if (upIndex === -1) {
reply.ups.push(userId);
success = true;
action = 'up';
} else {
reply.ups.splice(upIndex, 1);
action = 'down';
}
reply.save(function () {
res.send({
success: success
success: true,
action: action,
});
});
});

View File

@ -328,10 +328,16 @@
method: 'POST',
}).done(function (data) {
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;
if (data.action === 'up') {
$this.next('.up-count').text(currentCount + 1);
$this.addClass('uped');
} else {
alert('重复投票。');
if (data.action === 'down') {
$this.next('.up-count').text(currentCount - 1);
$this.removeClass('uped');
}
}
}
});
});