Mohammad Dayyan
Mohammad Dayyan

Reputation: 22458

How to update a property in an Javascript Object with ko.mapping - Knockout?

Assume we have the following Object :

var gridViewModelJs = 
    {"GridViewModel":{"Rows":[{"RowNumber":"1","Id":"6","Name":"FullNameOfUser","NumberOfUsers":"12","RegistrationDate":"10/15/2013"}],"FoundItems":"4","CurrentPage":1,"TotalPages":1,"ItemsPerPage":50,"PagingLinks":""}, 
    "EntityModel":{"Id":0,"PermissionIds":null,"Name":null,"NumberOfUsers":0,"PersianRegistrationDate":null,"RegistrationDate":"0001-01-01T00:00:00","Authorizations":null,"Users":null,"Contents":null}};
var KoEntityViewModel = ko.mapping.fromJS(gridViewModelJs);
ko.applyBindings(KoEntityViewModel);

Above code works, for updating the KoEntityViewModel. I used the following code :

// receivedData is data that returns from jQuery Ajax
// I'm dead sure `receivedData` is correct
var doneFunc = function (receivedData) {
    ko.mapping.fromJS(receivedData, KoEntityViewModel.EntityModel);
    showDetailsBlock();
};

But nothing update in KoEntityViewModel.EntityModel. Please guide me how I can update KoEntityViewModel.EntityModel in above sample

Upvotes: 1

Views: 108

Answers (1)

woz
woz

Reputation: 11004

When you update the mapping after applying bindings, use three parameters:

ko.mapping.fromJS(receivedData, {}, KoEntityViewModel);

Upvotes: 1

Related Questions