Knockout.js ko.mapping.toJS在我的视图中没有刷新数据 [英] Knockout.js ko.mapping.toJS not refreshing data in my view
问题描述
我从服务器获取一个json对象并填充我的视图。然后我更改数据,将其推回服务器。然后我获取数据的新副本,希望它会刷新我的视图并进行任何更改。然而,这不会发生。 TIA
I fetch a json object from the server and populate my view. I then change the data, push it back to the server. I then fetch a new copy of the data hoping it will refresh my view with any changes. However that doesn't happen. TIA
$(document).ready(function() {
var customer_id = get_customer_id();
var data = load_model();
contract_model = ko.mapping.fromJS(data,{});
ko.applyBindings(contract_model);
}
function load_model(){
var url = '/ar/contract_json?contract_id='+get_contract_id();
var data = '';
$.ajax({
type:'GET',
url:url,
async:false,
success: function(returningValue){
data = returningValue;
}
});
return data;
}
这个初始加载工作正常。我然后做一些事情并更改其中一个observable并将数据推回服务器。服务器获取更新,然后我重新获取数据以便视图刷新(我知道我可以一步传回新数据)但这在代码中我还没有重构)。
This initial load works fine. I then do some stuff and change one of the observables and push that data back to server. Server gets the update and then I do a new fetch of the data so that view will refresh (i know i can pass back the new data in one step but this in code i haven't refactored yet).
function refresh_data(contract_model){
var url = '/ar/contract_json?contract_id='+get_contract_id();
$.post(url,function(data){
console.log(data);
ko.mapping.fromJS(contract_model,{},data);
ko.applyBindings(contract_model);
console.log(ko.mapping.toJS(contract_model))
});
}
function refresh_data(contract_model){
var url = '/ar/contract_json?contract_id='+get_contract_id();
$.post(url,function(data){
console.log(data);
ko.mapping.fromJS(contract_model,{},data);
console.log(ko.mapping.toJS(contract_model))
});
}
function push_model(contract_model,refresh){
var url = '/ar/update_contract';
var data = {'contract':ko.mapping.toJSON(contract_model)}
delete data['lines'];
$.post(url,data,function(return_value){
if (refresh){
refresh_data(contract_model);
};
});
}
控制台消息全部显示新数据返回但我的视图永远不会更新。
The console messages all show the new data coming back but my view never updates.
推荐答案
我认为问题在于您传递给 ko.mapping.fromJS的参数顺序更新时的功能
contract_model
。
I believe the problem is with the order of parameters you pass into the ko.mapping.fromJS
function when you are updating contract_model
.
您有:
ko.mapping.fromJS(contract_model,{},data);
你想要的:
ko.mapping.fromJS(data, {}, contract_model);
这篇关于Knockout.js ko.mapping.toJS在我的视图中没有刷新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!