3
 var repo = {
     desc: 'this is repo desc.',
     title: 'this is title.',
     job: { jobId: 1, description: 'job desc', status: 'Done', workAllowed: 'Yes' },
     fault: { faultId: 1, jobId: 1, faultDescription: 'some thing', removed: 1, },
 };

 var mappingSettings = {
        'job': {
            create: function (options) {
                options.data.status = "this has been created.";
                return options.data;
            },
            update: function(options) {
                options.data.status = "this has been updated";
                return options.data;
            }

        }
    };


    $(document).ready(function () {
        viewModel = ko.mapping.fromJS(repo, mappingSettings);
        ko.applyBindings(viewModel);
    });

当我使用 mappingSetting 将 repo 映射到 viewModel 时,我发现 viewModel.status:"this has been updated"

4

1 回答 1

1

创建是第一次创建模型时,如果再次映射则更新触发器。

http://jsfiddle.net/pHQ9t/

viewModel = ko.mapping.fromJS(repo, mappingSettings);


//Update again
ko.mapping.fromJS(repo, mappingSettings, viewModel);
于 2012-11-29T10:29:12.703 回答