在淘汰赛中使用自定义映射创建和更新有什么区别?

时间:2012-11-29 06:53:30

标签: knockout.js knockout-mapping-plugin

 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:“这已经更新了”

1 个答案:

答案 0 :(得分:1)

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

http://jsfiddle.net/pHQ9t/

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


//Update again
ko.mapping.fromJS(repo, mappingSettings, viewModel);