2014-10-11 75 views
1

之後不會顯示正確的頁面結果下面的代碼允許用戶單擊按鈕並拒絕好友請求。即使在頁面上顯示多個結果,但它的功能正確,因爲它使用wrapper.children('.decline').click(function() {來定位正確的結果。JavaScript .load在.click

發生這種情況後,以下$("#containerFriends").load("friends.html #containerFriends");應該刷新頁面,以便顯示最新結果。但是,即使結果仍然存在,也會顯示完全空白的頁面。如果我手動刷新頁面,則會顯示正確的結果。

我不確定下面是什麼問題,是什麼導致這樣的問題?

mainQuery.find({ 
      success: function(results) { 
       var friends = []; 
       for (var i = 0; i < results.length; i++) { 
        friends.push({ 
         imageURL: results[i].get('toUser').get('pic'), 
         username: results[i].get('toUser').get('username'), 
         userId: results[i].get('toUser').id, 
         status: results[i].get('status'), 



         // Saves the object so that it can be used below to change the status// 
         fetchedObject: results[i] 


        }); 


       } 
       var select = document.getElementById("FriendsConnected"); 
       $.each(friends, function(i, v) { 
        var opt = v.username; 
        var el = document.createElement("option"); 
        el.textContent = opt; 
        el.value = opt; 
        select.appendChild(el); 
       }) 


       $('#containerFriends').empty(); 
       $('#containerFriendsConnected').empty(); 

       _.each(friends, function(item) { 
        var wrapper = $('<div class="portfolio-item-thumb one-third"></div>'); 
        wrapper.append('<img class="responsive-image friendImgOutline" src="' + item.imageURL + '" />' + '<br>'); 
        wrapper.append('<div class="tag">' + item.username + '</div>'); 
        wrapper.append('<div type="button" class="btn btn-danger mrs decline">' + 'Unfriend' + '</div>'); 

        $('#containerFriends').append(wrapper); 



        //The following lets the user accept or decline a friend request by changing the status the status from Pending to Declined///// 
        wrapper.children('.decline').click(function() { 
         $(".decline").click(function() { 
          item.fetchedObject.set("status", "Rejected"); 
          $("#containerFriends").load("friends.html #containerFriends"); 


          item.fetchedObject.save(null, { 
           success: function(results) { 
            console.log("REJECTED"); 

           }, 
           error: function(contact, error) { 
            // The save failed. 
            // error is a Parse.Error with an error code and description. 
            alert("Error: " + error.code + " " + error.message); 
           } 
          }); 


         }); 


        }); 

       }); 

      }, 
      error: function(error) { 
       alert("Error: " + error.code + " " + error.message); 
      } 
     }); 

回答

0

我實際上是通過再次調用函數來解決這個問題的,這確保了數據是最新的並刷新了頁面。我相信問題是$('#containerFriends').append(wrapper); did'nt不包含更新後的數據用戶.click