2016-11-06 15 views
1

我需要兩個服務器變量只在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") 
+0

你有沒有想過使用AJAX來獲取更新的變量? – Dez

+0

@Dez我會怎麼做?我對AJAX並不是很有經驗。你可以使用AJAX發佈解決方案作爲答案,我可以接受它嗎? – Coder1000

+0

檢查有關jQuery AJAX對node.js的調用:https://stackoverflow.com/questions/5373987/how-to-use-jquery-ajax-calls-with-node-js,除了我提供的答案。 – Dez

回答

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 
} 
+0

我發現了一個更優雅的解決方案,通過使用局部變量來解決我的問題,儘管感謝您的輸入,但它絕對正確! – Coder1000

1

以在計數我不是在node.js的經歷,你應該做的是更換$('.HP').text("<%= post.upvotes - post.downvotes %> HP");線AJAX調用你在哪裏得到更新的變量和使用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'}) }); 
}); 

我是不是要能夠顯示一個確切的答案運作對不起。

+0

試試幫助我:) – Coder1000

相關問題