The following part is clear enough.
viewModel.loadUserData = function () {
$.getJSON("/get-user-data", function (data) {
// update the data in existing ViewModel.
ko.mapping.fromJS(data, viewModel);
});
} |
viewModel.saveUserData = function() {
// Convert the viewModel into JSON.
var data_to_send = {userData: ko.toJSON(viewModel)};
// Send that JSON data to server.
$.post("/save-user-data", data_to_send, function(data) {
alert("Your data has been posted to the server!");
});
} |
Hi Paul,Thanks for contacting Syncfusion support,We have checked your query, the loadUserData and saveUserData both are separate methods. loadUserData method is used to map the JSON objects which is loaded from the server to ViewModel observables, which is updating in the existing ViewModel.loadUserData method:
viewModel.loadUserData = function () {$.getJSON("/get-user-data", function (data) {// update the data in existing ViewModel.ko.mapping.fromJS(data, viewModel);});}saveUserData method is used to convert the ViewModel into JSON and send that to server. You can call both methods separately.saveUserData method:
viewModel.saveUserData = function() {// Convert the viewModel into JSON.var data_to_send = {userData: ko.toJSON(viewModel)};// Send that JSON data to server.$.post("/save-user-data", data_to_send, function(data) {alert("Your data has been posted to the server!");});}Please let us know if you need further assistance.Regards,Shanmugaraja K
<div class='sample'>
<p>Load: <input type="button" value="Load User Data" data-bind="click: loadUserData" /></p>
<p>Name: <input data-bind='value: firstName' /></p>
<p>Save: <input type="button" value="Save User Data" data-bind="click: saveUserData" /></p>
</div>
<script>
$(document).ready(function () {
var viewModel = {};
viewModel.firstName = ko.observable('Knockout JS');
viewModel.loadUserData = function () {
$.getJSON("/data.json", function (data) {
// update the data in existing ViewModel.
ko.mapping.fromJS(data, viewModel);
viewModel.firstName(data.name);
});
};
viewModel.saveUserData = function () {
// Convert the viewModel into JSON.
var data_to_send = { userData: ko.toJSON(viewModel) };
// Send that JSON data to server.
$.post("/save-user-data", data_to_send, function (data) {
alert("Your data has been posted to the server!");
}).fail(function () {
alert("Ensure the Url before save the data");
});
};
ko.applyBindings(viewModel);
});
</script>
|
I'm confused by your answer.
Especially this statement:
"viewModel.firstName = ko.observable('Knockout JS');"
I'm confused because of two reasons:
in "Knockout_Succintly.pdf", on the bottom page 58- top of page 59 it reads:
"we’ll use jQuery’s $.getJSON() method to load some initial data from the server
and let the mapping plug-in dynamically generate observables"
Aside of the complexity of your example (number of files in a visual studio setting), I'm also confused by your example itself.
I've been following the PDF file from beginning to end. In general it's quit clear except for our current conversation topic.
Chapter 7 Accessing External Data, starts with an example without the mapping plugin.
Following the text, at page 58, just before "Mapping Data tot ViewModels", the html file (in my filesystem) looks like (zie attachment: "ExternalData.html"
Further there is a file named "get-user-data" containing one JSON format record (refer to top of page 57 )
This example works just fine.
Now my problem starts on top of page 59.
I've to replace the entire custom <script> (of ExternalData.html) with the new script on top of page 59.
Of course that isn't too difficult
The result is that the JSON data is loaded at loading the page.
Pushing the Load Data or Save Data buttons looks like the fields reload(?)
Trying to incorporate the load and save methods doesn't work.
Even when I used your suggestion to use $( document ).ready()
and it looks like the bindings couldn't be parsed (dev console)
So what's wrong
<form action="#" method="post">
<p>First name: <input data-bind='value: firstName' /></p>
<p>Last name: <input data-bind='value: lastName' /></p>
<div>
Your favorite hobby:
<select data-bind='options: activities, value: favoriteHobby'></select>
</div>
<p><button data-bind='click: $root.loadUserData'>Load Data</button></p>
<p><button data-bind='click: $root.saveUserData'>Save Data</button></p>
</form>
<script type='text/javascript'>
function PersonViewModel() {
var self = this;
self.firstName = "";
self.lastName = "";
self.activities = [];
self.favoriteHobby = "";
self.loadUserData = function () {
$.getJSON("/get-user-data.json", function (data) {
var viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
}
self.saveUserData = function () {
var data_to_send = { userData: ko.toJSON(self) };
$.post("/save-user-data", data_to_send, function (data) {
alert("Your data has been posted to the server!");
}).fail(function () {
alert("Ensure the Url before save the data");
});
}
}
ko.applyBindings(new PersonViewModel());
</script>
|
var viewModel = {};
viewModel.loadUserData = function () {
$.getJSON("get-user-data.json", function (data) {
ko.mapping.fromJS(data, viewModel);
});
}
viewModel.saveUserData = function () {
var data_to_send = { userData: ko.toJSON(viewModel) };
alert(data_to_send.userData);
$.post("save-user-data", data_to_send, function (data) {
alert("Your data has been posted to the server!");
});
}
$.getJSON("get-user-data.json", function (data) {
ko.mapping.fromJS(data, {}, viewModel);
ko.applyBindings(viewModel);
});
|
$.getJSON("get-user-data.json", function (data) {
var viewModel = ko.mapping.fromJS(data);
viewModel.loadUserData = function () {
$.getJSON("get-user-data.json", function (data) {
ko.mapping.fromJS(data, viewModel);
});
};
viewModel.saveUserData = function () {
var data_to_send = { userData: ko.toJSON(viewModel) };
alert(data_to_send.userData);
$.post("save-user-data",
data_to_send,
function (data) {
alert("Your data has been posted to the server!");
}
);
}
ko.applyBindings(viewModel);
});
|
[JS]
$.getJSON("get-user-data.json", function (data) {
//…
viewModel.saveUserData = function () {
var data_to_send = { userData: ko.toJSON(viewModel) };
// SaveData.asmx - Web Service name; Save_User_Data - method name.
// datavalue- The data which send to server
$.post("SaveData.asmx/Save_User_Data", { datavalue: data_to_send.userData },
function (data) {
if (data == "Success")
alert("Your data has been posted to the server!");
else
alert("Failed to update!");
}
);
}
ko.applyBindings(viewModel);
});
|
public class SaveData : WebService
{
[WebMethod]
[ScriptMethod( ResponseFormat = ResponseFormat.Json)]
public void Save_User_Data()
{
// datavalue - Received from client side.
string data = HttpContext.Current.Request.Params["datavalue"];
string path = Server.MapPath("~/JsonFile/");
try
{
//write the data into the SavedData.json file.
File.WriteAllText(path + "SavedData.json", data);
Context.Response.Write("Success");
}
catch
{
Context.Response.Write("Failure");
}
}
}
|
public class SaveData : WebService { [WebMethod] |