I have a function getGeneral
that calls ajax get. When ajax receives the data (json), it creates a KO model from the given json and returns the created KO.
When a knockout model is created and values ββare assigned, a knockout is called applybindings
. Here is my code:
Defines GeneralModel
some related functions (inside " GeneralModel.js
"):
var GeneralModel = function() {
}
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) {
}
});
}
This is called from another file (GeneralTabl.html), it must call the get function applybindings
to update the interface:
var PortfolioGeneral = getGeneral("@Model.Id");
ko.applyBindings(PortfolioGeneral, document.getElementById("pv-portfolio-general-tab"));
However, in this case I get an error (CountryName not defined). This is because it applybindings
happens before ajax returns the data, so I do applyBindings for an empty model with undefined properties.
Json to Model : p = ko.mapping.fromJS(item);
GeneralModel , ( ):
var GeneralModel = function() {
CountryName = ko.observable();
...
}
"CountryName ".
?
1) - getGeneral
GeneralModel
, GeneralModel?
2) , - " ajax" applybindings
?
, , KO JS.
. , , Ajax - Async, , , , getGeneral .