2016-11-06 1 views
1

dbの変数を変更した後にのみ、2つのサーバー変数をロードする必要があります。特定のイベントの後にのみサーバー変数をロードする方法はありますか?

今は、ページがロードされるとすぐにロードされるため、イベントの前にあった値を保持します(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:

私は私の問題を解決するために、ローカル変数を作成することになりました。あなたの答えをありがとう!

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を使用しての考えたことがあり:
は、あなたのコールバックは2番目のパラメータとしてすべきですか? – Dez

+0

@Dezどうすればいいですか?私はAJAXであまり経験がありません。私はそれを受け入れることができるように答えとしてAJAXを使用してソリューションを投稿できますか? – Coder1000

+0

ここでnode.jsのjQuery AJAX呼び出しを確認してください: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の中で経験したわけではない数に取っては、何をやるべきことは、あなたが更新された変数を取得し、jQueryを使ってそれらを追加AJAX呼び出しの$('.HP').text("<%= post.upvotes - post.downvotes %> HP");ラインを交換です。これらの線に沿って何か:私は正確に機能している答えを示すことができるにされていないため申し訳ありません

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'}) }); 
}); 

node.js側で

$('.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(); 
      ... 
} 

あなたはこれらの線に沿って何かが必要になります。

+0

私に手伝ってもらうためにupvoteを持っています:) – Coder1000

関連する問題