2014-10-11 55 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