Mvvm 敲除传递给函数的数据在AJAX调用后不可用
我发现自己有点大了。在一个淘汰的ViewModel中,我触发了一个到服务器的ajax帖子,在成功响应后,我将更新ViewModel。在其中一个方法上,我无法访问触发事件的对象之前,此方法工作正常 工作示例:Mvvm 敲除传递给函数的数据在AJAX调用后不可用,mvvm,knockout.js,viewmodel,Mvvm,Knockout.js,Viewmodel,我发现自己有点大了。在一个淘汰的ViewModel中,我触发了一个到服务器的ajax帖子,在成功响应后,我将更新ViewModel。在其中一个方法上,我无法访问触发事件的对象之前,此方法工作正常 工作示例: selfVM.SubmitCommentEvent = function (data, event) { var comment = { UserId: selfVM.userID(), OpponentRegistrationID: data.ID,
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");
}
}
selfVM.SubmitCommentEvent=函数(数据、事件){
var注释={
UserId:selfVM.UserId(),
OpponentRegistrationID:data.ID,
内容:data.CommentMessage
};
$('.comment_btn:hover').addClass('loading');
if(comment.UserId){
$.ajax({
url:“/FindOpponent/PostComment”,
键入:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
数据:JSON.stringify(注释),
成功:功能(结果){
如果(结果==真){
对于(变量i=0;i
使用此方法,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");
}
});
}
selfVM.JoinEvent=函数(数据、事件){
变量参数={
opponentRegistrationID:data.ID,
message:data.JoinMessage,
UserID:selfVM.UserID()
};
$('.Join_btn:hover').addClass('loading');
$.ajax({
url:“/findoponent/JoinRegistration”,
键入:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
数据:JSON.stringify(参数),
数据类型:“json”,
成功:功能(结果){
如果(结果==真){
data.ID????????????
提醒(“您已成功注册。谢谢!”);
$('.Join_btn').removeClass('loading');
}
否则{
警报(“您已注册”);
$('.Join_btn').removeClass('loading');
}
},
错误:函数(e){
$('.Join_btn').removeClass('loading');
警告(“插入错误”);
}
});
}
我收到的数据(对象)不可用。如果有人能澄清这一点,我将不胜感激
谢谢。如何以及从何处调用
JoinEvent
?ko单击绑定到模板上元素的事件我做了修复。我在虚拟机上使用了一个临时属性,工作得很好。