javascript - How to load a server variable only after a specific event? -
i need 2 server variables loaded after var change in db.
right now, load when page loaded , therefore keep value had before event (upvotesref.transaction
).
$('.hp').text("<%= post.upvotes - post.downvotes %> hp");
code:
$('.upvotebutton').click(function () { var $this = $(this); if ($this.hasclass("on")) { $this.removeclass("on"); upvotesref.transaction(function (upvotes) { if (!upvotes) { upvotes = 0; } upvotes = upvotes - 1; return upvotes; }); userref.remove(); $('.hp').text("<%= post.upvotes - post.downvotes %> hp");
edit:
i ended creating local variables solve problem. thank answers !
var upvoteslocal = <%= post.upvotes %>; var downvoteslocal = <%= post.downvotes %>; $('.upvotebutton').click(function () { var $this = $(this); if ($this.hasclass("on")) { $this.removeclass("on"); upvotesref.transaction(function (upvotes) { if (!upvotes) { upvotes = 0; } upvotes = upvotes - 1; return upvotes; }); upvoteslocal = upvoteslocal -1 userref.remove(); $('.hp').text((upvoteslocal - downvoteslocal) + " hp")
you should , update variables after transaction right?
means should happen in callback.
assume using firebase transaction method.
callback should second parameter:
$('.upvotebutton').click(function () { var $this = $(this); if ($this.hasclass("on")) { $this.removeclass("on"); upvotesref.transaction(function (upvotes) { if (!upvotes) { upvotes = 0; } upvotes = upvotes - 1; return upvotes; }, updatevotes); //<-- here callback function added userref.remove(); function updatevotes(error, iscommitted, upvotes) { //here got new upvotes //but should request downvotes, //here need ajax request, see e.g. dez's answer or comments }
Comments
Post a Comment