From 2267b8c3061a6d7d08e847c312d317cb15d59c0a Mon Sep 17 00:00:00 2001 From: alsotang Date: Sun, 7 Sep 2014 15:08:56 +0800 Subject: [PATCH] hotfix --- controllers/reply.js | 13 +++++++++---- views/topic/index.html | 14 ++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/controllers/reply.js b/controllers/reply.js index 3f52717..d622114 100644 --- a/controllers/reply.js +++ b/controllers/reply.js @@ -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, }); }); }); diff --git a/views/topic/index.html b/views/topic/index.html index 2b35344..c1196a3 100644 --- a/views/topic/index.html +++ b/views/topic/index.html @@ -328,10 +328,16 @@ method: 'POST', }).done(function (data) { if (data.success) { - $this.next('.up-count').text((+$this.next('.up-count').text || 0) + 1); - $this.addClass('uped'); - } else { - alert('重复投票。'); + 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 { + if (data.action === 'down') { + $this.next('.up-count').text(currentCount - 1); + $this.removeClass('uped'); + } + } } }); });