如何在特定事件之后加载服务器变量?

时间:2016-11-06 00:27:59

标签: javascript jquery node.js

我需要在db中的var更改后才加载两个服务器变量。

现在,它们会在加载页面时立即加载,因此保留它们在事件之前的值(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");

编辑:

我最终创建了局部变量来解决我的问题。谢谢你的回答!

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")

2 个答案:

答案 0 :(得分:2)

你应该在交易权之后得到并更新变量吗? 这意味着它应该在您的回调中发生 我假设您使用的是firebase交易方法 那么你的回调应该是第二个参数:

$('.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 the callback function added
            userRef.remove();

function updateVotes(error, isCommitted, upvotes) {
   //here you got already your new upvotes
   //but you should request the downvotes,
   //here you need the ajax request, see e.g. Dez's answer or comments
}

答案 1 :(得分:1)

考虑到我在node.js中没有经验,你应该做的是替换AJAX调用的$('.HP').text("<%= post.upvotes - post.downvotes %> HP");行,在那里你获得更新的变量并用jQuery添加它们。这些方面的东西:

$('.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; 
            }); 
            $.ajax({
                type : 'POST',
                data: { userRef: userRef},
                contentType: 'application/json',
                url: 'http://localhost:3000/endpoint',      
                success: function(data) {
                    console.log('success');
                    console.log(JSON.stringify(data));
                    /* Do whatever you need with your data, for example*/
                    $('.HP').text(data.upvotes + "-" + data.downvotes + "HP");
                },
                error : function (event) {
                    console.log(event);
                }
            });
            userRef.remove();
            ...
}

node.js方面,你需要这些内容:

app.post('/endpoint', function(req, res) {
  console.log(req.body.userRef)
  /* Get the upvotes and downvotes and send them back in the response */
  res.contentType('json');
  res.send({ some: JSON.stringify({response:'json'}) });
});

我很抱歉无法显示确切正常的答案。