Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between create and update with customizing mapping in knockout?

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

When i map the repo to viewModel using mappingSetting, i found viewModel.status:"this has been updated"

like image 433
Cruiser KID Avatar asked Feb 01 '26 09:02

Cruiser KID


1 Answers

Create is the first time when you create the model, update triggers if you map again.

http://jsfiddle.net/pHQ9t/

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


//Update again
ko.mapping.fromJS(repo, mappingSettings, viewModel);
like image 181
Anders Avatar answered Feb 03 '26 09:02

Anders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!