0

I have found my self in a bit of a biggy. In a knockout ViewModel I am triggering a ajax post to server and upon a successful response I am updating the ViewModel. This method is working fine until on one of them I have no access to the object that has triggered the event.

WORKING EXAMPLE :

selfVM.SubmitCommentEvent = function (data, event) {
    var comment = {
        UserId: selfVM.userID(),
        OpponentRegistrationID: data.ID,
        Content: data.CommentMessage
    };
    $('.comment_btn:hover').addClass('loading');
    if (comment.UserId) {
        $.ajax({
            url: "/FindOpponent/PostComment",
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            data: JSON.stringify(comment),
            success: function (result) {
                if (result == true) {
                    for (var i = 0; i < initialArray.length; i++) {
                        if (initialArray[i].ID == data.ID) {
                            initialArray[i].Comments.push({ Content: data.CommentMessage, UserName: data.UserName, UserPhoto: data.UserPhoto });
                            selfVM.opponentResult(eval(JSON.stringify(initialArray)));
                        }
                    }
                    //selfVM.opponentResult(eval(JSON.stringify(initialArray)));
                $('.comment_btn').removeClass('loading');
                    selfVM.keepVisibleComment(data);
                }
            },
            error: function (err) {
                $('.comment_btn').removeClass('loading');
                alert("Could not post message");
            }
        });
        return true;
    } else {
        alert("Please login in order to post a comment");
    }
}

With this method live update of the UI is working fine and I have access to the object. But with this one :

selfVM.JoinEvent = function (data, event) {
    var parameters = {
        opponentRegistrationID: data.ID,
        message: data.JoinMessage,
        UserID: selfVM.userID()
    };
    $('.Join_btn:hover').addClass('loading');
    $.ajax({
        url: "/FindOpponent/JoinRegistration",
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(parameters),
        dataType: 'json',
        success: function (result) {
            if (result == true) {
                            data.ID ???????????? <not available> 

                            alert("You have successfuly registered. ThankYou !");
                $('.Join_btn').removeClass('loading');
            }
            else {
                alert("You are already registered");
                $('.Join_btn').removeClass('loading');
            }
        },
        error: function (e) {
            $('.Join_btn').removeClass('loading');
            alert("Error do Insert");
        }
    });
}

i am receiving that data(the object) is not available. Please if can anyone clarify this I would be grateful.

Thank You.

4

0 回答 0