使用JS .load的替代方法?

时间:2014-10-11 14:19:18

标签: javascript jquery

是否可以使用.load来刷新Div的内容?基本上我在下面使用.load来删除对象后重新加载div。但目前这还没有正常工作。

.load返回没有结果的div,即使其中应该有对象。当我手动刷新页面时,显示正确的结果。

因此,我可以从用户点击的视图中删除对象吗?

     /////////////////////SECTION 1 - This code block returns the current users friends to the page. THIS IS NOT USED IN THE FILTER//////
    $('#containerFriends').empty();
    $('#friendsProfile').hide();
    $('#container').empty();
    $('#containerFriendsConnected').empty();


    var currentUser = Parse.User.current();
    var FriendRequest = Parse.Object.extend("FriendRequest");

    var queryOne = new Parse.Query(FriendRequest);
    queryOne.equalTo("toUser", currentUser);


    var queryTwo = new Parse.Query(FriendRequest);
    queryTwo.equalTo("fromUser", currentUser);

    var mainQuery = Parse.Query.or(queryOne, queryTwo);
    mainQuery.include('toUser');
    mainQuery.include('fromUser');
    mainQuery.equalTo("status", "Connected");


    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 个答案:

没有答案