This is a migrated thread and some comments may be shown as answers.

Saving the MVVM model

3 Answers 202 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve asked on 02 Apr 2012, 03:12 AM
Can someone tell me whats the best way to send a rather complex MVVM object up through an $.ajax call to be saved?

...this?

data: "{'data':'" +JSON.stringify(_mydata) +"'}",

3 Answers, 1 is accepted

Sort by
0
Accepted
Randy
Top achievements
Rank 1
answered on 02 Apr 2012, 04:30 AM
I am no expert on this, just got it working a few days ago, but here is the code I use.

I had two problems, I had to use a POST dues to the complexity of the data being sent and I had to deal with dates. On read I moved the dates to a shadow field and then on save I moved them back, converting to and from WCF / Javascript Dates.

$(".clickSave").click(function (e) {
    tg.viewModel.data.ApplicationCreatedOn = dateToWcf(tg.viewModel.data.shadowApplicationCreatedOn);
    tg.viewModel.data.LastUpdatedOn = dateToWcf(tg.viewModel.data.shadowLastUpdatedOn);
    tg.viewModel.data.DeclarationDateTime = dateToWcf(tg.viewModel.data.shadowDeclarationDateTime);
    tg.viewModel.data.ApplicantDob = dateToWcf(tg.viewModel.data.shadowApplicantDob);
    tg.viewModel.data.IsDirty = true;
 
    $.ajax({
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        url: tg.crudServiceBaseUrl + "/DDCApplicationSave",
        data: JSON.stringify(tg.viewModel.data),
        processData: true,
        dataType: "json",
        success: function (data) {
            if (data == "") {
                toast("You application as been saved to our servers.");
            } else {
                alert("An error occured while saving the application. \n\n The error was: " + data + "\n\n If you close your browser now, any unsaved data will be lost.");
            };
        }
    });
});


Hope this helps
Randy
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 02 Apr 2012, 04:46 AM
Ahha, okay thanks...I think I can run with that :)
0
axwack
Top achievements
Rank 1
answered on 25 Aug 2012, 02:44 AM
I have been using the datasource and there is a parameterMap that can do the data manipulation for you. If you go the BLOGS there is an example of using it in the CRUD demo.

What I'm struggling with is how to set data that has a function associated with it. 
Tags
MVVM
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
Randy
Top achievements
Rank 1
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
axwack
Top achievements
Rank 1
Share this question
or