Adding properties to the view model created by using the Knockout JS mapping plugin

前端 未结 4 919
说谎
说谎 2020-12-02 13:41

I am working through the mapping plugin example on the Knockoutjs website.

This is the example data.

Knockout JS Mapping Plugin

var data = {
         


        
4条回答
  •  情书的邮戳
    2020-12-02 13:47

    Here is a continuation to this answer based on RP Niemeyer's solution

    This answer here is based on the solution above and from his blog--Thanks for that! I thought I should add some details because it addresses when the array is not a first level object.

     var data = {
    person: {
           children: [{ id: 1, firstName: 'Scot', lastName: 'Weise'}]
        }
    };
    
    var mapping = {
        'children': {
            create: function(options) {
                return (new (function() {
                    // setup the computed binding for some reason I had
                    //  to use ko.dependentObservable instead of ko.computed
                    // or errors showed up.
                    this.fullName = ko.dependentObservable(function() {
                        return this.firstName() + ' ' + this.lastName();
                        }, this);
                        ko.mapping.fromJS(options.data, { }, this);
                    })(/* call the ctor here */));
                }
            }
        };
    
        // Create view model and map the data object to the model.
        var viewModel = ko.mapping.fromJS(data, {});
    
        // update the viewModel with the mapping object that contains 
        // some a computed property(s)
        viewModel.person = ko.mapping.fromJS(viewModel.person, mapping);
        ko.applyBindings(viewModel);
    

    Note that the person is the first level object and children is a sub property of that person. The line viewModel.person = ko.mapping.fromJS(viewModel.person, mapping) wasn't intutive to me at first.

    And here is a slight variation

    The person object is an observable that is added or updated after it is originally created from the server json data.

    var viewModel = {};
    $(document).ready(function () {
        var person = getPerson();
    
        // selected person is added to the viewModel
        viewModel.selectedPerson = ko.observable(person);
        ko.applyBindings(viewModel);
    });
    
    function getPerson() { 
    // you would probably pass this into the function as a parameter. 
    var person =
        {
            name: 'jim',
            children: [{ id: 1, firstName: 'jane', lastName: 'bob'}]
        };
    
        var mapping = {
            'children': {
                create: function (options) {
                    return (new (function () {
                        // setup the computed binding
                        this.fullName = ko.dependentObservable(function () {
                        return this.firstName() + ' ' + this.lastName();
                        }, this);
                        ko.mapping.fromJS(options.data, {}, this);
                    })(/* call the ctor here */));
                }
            }
        };
    
        var result = ko.mapping.fromJS(person, mapping);
        return result;
    }
    

    Some binding code in the html

    Ultimately, you'll need to put it to some use at some point like this:

    Thank for your help! I couldn't have made it this far without your blog post.

提交回复
热议问题