如何使用ko.mapping更新Javascript对象中的属性 - Knockout?

时间:2013-10-15 17:27:41

标签: javascript knockout.js mapping

假设我们有以下对象:

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);

以上代码有效,为了更新KoEntityViewModel.,我使用了以下代码:

// 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();
};

KoEntityViewModel.EntityModel中没有任何更新。 请指导我如何在上面的示例中更新KoEntityViewModel.EntityModel

1 个答案:

答案 0 :(得分:1)

在应用绑定后更新映射时,请使用以下三个参数:

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