I have this JSON data coming from the server:
这个JSON数据来自服务器:
{"HaveNotification":false,"IsError":false,"Title":null,"Description":null}
and am trying to populate this view model via ko.mapping:
我正在尝试通过ko.mapping来填充这个视图模型:
var notifyVM = {
HaveNotification: ko.observable(true),
IsError: ko.observable(false),
Title: ko.observable('Title goes here'),
Description: ko.observable('Description goes here'),
}
with this code, which is called on a polling interval:
使用此代码,在轮询间隔中调用:
function pollNotifications() {
$.getJSON('@Url.Action("GetNotifications", "Home")', function (data) {
ko.mapping.fromJSON(data, notifyVM);
setTimeout(pollNotifications, 10000);
});
}
and this is page load code:
这是页面加载代码:
$(function () {
ko.applyBindings(notifyVM);
setTimeout(pollNotifications, 10000);
});
but its not working. If I inspect the view model after the fromJSON call the observables are not updated, they are still at their initial values.
但它不工作。如果我在fromJSON调用后查看视图模型,那么它们仍然是初始值。
UPDATE: Some more info... if I do this in the pollNotifications function
更新:更多信息…如果我在pollnotification函数中这样做。
var newVM = ko.mapping.fromJSON(data);
I notice that the view model it creates is not the same as my one, it consists of a single observable function, whereas mine is an object with a set of observable properties.
我注意到它创建的视图模型与我的模型不同,它由一个可观察的函数组成,而我的模型是一个具有一系列可观察属性的对象。
3 个解决方案
#1
8
Try:
试一试:
ko.mapping.fromJS(data, {}, notifyVM);
The two parameter form expects a view-model that was previously created with ko.mapping.fromJS. If it doesn't find one (based on looking for a specific property that fromJS adds), it assumes you're calling the fromJS(data, options) form.
The 3 parameter form removes this ambiguity.
这两个参数表单期望一个之前由ko.mapp.fromjs创建的视图模型。如果没有找到(基于查找fromJS添加的特定属性),则假定您正在调用fromJS(数据、选项)表单。3参数形式消除了这种模糊性。
#2
0
Have you tried manually mapping the values to the view model and seeing if that works, like this?
您是否尝试过手动地将值映射到视图模型并查看它是否有效,比如这样?
function pollNotifications() {
$.getJSON('@Url.Action("GetNotifications", "Home")', function (data) {
notifyVM.HaveNotification(data.HaveNotification);
notifyVM.IsError(data.IsError);
notifyVM.Title(data.Title);
notifyVM.Description(data.Description);
setTimeout(pollNotifications, 10000);
});
}
If the above does not work, then you know it is an issue with your data; otherwise the mapping is not happening correctly with ko.mapping.fromJSON()
.
如果上述方法不起作用,那么您就知道这是您的数据的问题;否则,在ko.mapp . fromjson()中映射不会正确地发生。
#3
0
You should use ko.mapping.fromJS instead of ko.mapping.fromJSON because $.getJSON is returning a data object (not an json string):
应该使用ko.mapp . fromjs而不是ko.mapp . fromjson,因为是$。getJSON返回的是一个数据对象(不是json字符串):
The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method. It is also passed the text status of the response.
返回的数据通常是一个JavaScript对象或数组,由JSON结构定义并使用$. parsejson()方法解析。它还传递响应的文本状态。
http://api.jquery.com/jquery.getjson/
http://api.jquery.com/jquery.getjson/
do a console.log(data); to see the difference
做一个console.log(数据);看到的区别
#1
8
Try:
试一试:
ko.mapping.fromJS(data, {}, notifyVM);
The two parameter form expects a view-model that was previously created with ko.mapping.fromJS. If it doesn't find one (based on looking for a specific property that fromJS adds), it assumes you're calling the fromJS(data, options) form.
The 3 parameter form removes this ambiguity.
这两个参数表单期望一个之前由ko.mapp.fromjs创建的视图模型。如果没有找到(基于查找fromJS添加的特定属性),则假定您正在调用fromJS(数据、选项)表单。3参数形式消除了这种模糊性。
#2
0
Have you tried manually mapping the values to the view model and seeing if that works, like this?
您是否尝试过手动地将值映射到视图模型并查看它是否有效,比如这样?
function pollNotifications() {
$.getJSON('@Url.Action("GetNotifications", "Home")', function (data) {
notifyVM.HaveNotification(data.HaveNotification);
notifyVM.IsError(data.IsError);
notifyVM.Title(data.Title);
notifyVM.Description(data.Description);
setTimeout(pollNotifications, 10000);
});
}
If the above does not work, then you know it is an issue with your data; otherwise the mapping is not happening correctly with ko.mapping.fromJSON()
.
如果上述方法不起作用,那么您就知道这是您的数据的问题;否则,在ko.mapp . fromjson()中映射不会正确地发生。
#3
0
You should use ko.mapping.fromJS instead of ko.mapping.fromJSON because $.getJSON is returning a data object (not an json string):
应该使用ko.mapp . fromjs而不是ko.mapp . fromjson,因为是$。getJSON返回的是一个数据对象(不是json字符串):
The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method. It is also passed the text status of the response.
返回的数据通常是一个JavaScript对象或数组,由JSON结构定义并使用$. parsejson()方法解析。它还传递响应的文本状态。
http://api.jquery.com/jquery.getjson/
http://api.jquery.com/jquery.getjson/
do a console.log(data); to see the difference
做一个console.log(数据);看到的区别