我有getGeneral调用ajax GET的函数.当ajax收到数据(json)时,它会从给定的json创建KO模型并返回创建的KO.
创建Knockout模型并分配值时,applybindings应调用knockout .这是我的代码:
定义GeneralModel和一些相关的函数(在" GeneralModel.js" 里面):
var GeneralModel = function() {
//for now it is empty as data ar binded automatically from json
// CountryName is one of the properties that is returned with json
}
function getGeneral(pid) {
$.ajax({
url: "/api/general",
contentType: "text/json",
dataType: "json",
type: "GET",
data: { id: pid},
success: function (item) {
var p = new GeneralModel();
p = ko.mapping.fromJS(item);
return p;
},
error: function (data) {
} …Run Code Online (Sandbox Code Playgroud)