Knockout mapping with computed fields

I get data from the WCF service, and I map and bind the data to my DOM object:

var PayinyVM = {};

    $.getJSON('/service/PaidService.svc/PaidList', function (data) {
        var tmp = JSON.stringify(data.d);

        PayinyVM.model = ko.mapping.fromJSON(tmp);
        ko.applyBindings(PayinyVM);
    }); 

the result is shown as excluded from my DOM, bind it to the model. What I could not figure out was to add some computable observables, let them say that my data returns people with FirstName and LastName, how can I make a calculated observable FullName with FN + '' + LN.

+5
source share
3 answers

Here is a working copy of your violin, I had to make a lot of assumptions, since your violin was not even the correct javascript and seemed rather confusing and did not even turn to knockout

var PaidPeople = function(data) {
    var self = this;
    ko.mapping.fromJS(data, {}, this);
    this.fullName = ko.computed(function () {
                    return self.Name() + " : just ";
                });
}

var PayinyVM = function (data) {
                var self = this;

                ko.mapping.fromJS(data, {
                    'model' : {
                        create: function(options) {
                            return new PaidPeople(options.data);
                    }                        
                  }
                }, self);                
            };

var data = {model:[{__type: "PaidPeople:#model", Amount:110, Attendee:1, Name:'John'}]};

ko.applyBindings(new PayinyVM(data)); ​

, : http://jsfiddle.net/qeUHd/

+9

, , .

var PayinyVM = function (data) {
    var self = this;
    ko.mapping.fromJS(data, {}, self);
    this.fullName = ko.computed(function () {
        return self.Name() + " : just ";
    });
};

$.getJSON('/service/PaidService.svc/PaidList', function (data) {    
    ko.applyBindings(new PayinyVM(data.d));
});

, .

+4

It turns out I need to define all the properties of the view model inside javascript so that the knockout can initialize the view model with properties before updating it with the server data

Link: http://www.underwatergorilladome.com/how-to-use-knockouts-computed-observables-with-the-mapping-plugin/

http://jsfiddle.net/GLDxx/2/

var model = {
    username : ko.observable(),
    get_student_info : ko.mapping.fromJS(
        {
            usr_lname : null,
            usr_fname : null,
            gender : null,
            dob : null
        },
        {
            create: function(options) {
                return (new (function () {
                    this.name = ko.computed(function () {
                        if (this.usr_lname == undefined || this.usr_fname == undefined)
                            return null;
                        else
                            return this.usr_lname() + ' ' + this.usr_fname(); 
                    }, this);

                    // let the ko mapping plugin continue to map out this object, so the rest of it will be observable
                    ko.mapping.fromJS(options.data, {}, this);
                }));
            }
        }
    )
};
+2
source

All Articles