我发现我的自我有点大。在一个淘汰赛ViewModel中,我正在触发ajax发布到服务器,并且在成功响应后,我正在更新ViewModel。这个方法工作正常,直到其中一个我无法访问触发事件的对象。
工作示例:
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");
}
}
使用此方法,UI的实时更新工作正常,我可以访问该对象。但有了这个:
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");
}
});
}
我收到的数据(对象)不可用。如果有人能澄清这一点,我将不胜感激。
谢谢。