knockout mapping with computed fields

旧街凉风 提交于 2019-12-18 12:37:05

问题


I'm getting data from a WCF service,and i map, and bind the data with 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 excepted on my DOM bind it to model. What I couldn't find out is how to add some computed observable let's say my data is returning people with FirstName and LastName, how can I make a computed observable FullName with FN + ' ' + LN.


回答1:


Here's a working copy of your fiddle, I had to make a lot of assumptions as your fiddle wasn't even correct javascript and seemed quite confused and didn't even reference 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)); ​

and a fiddle that works : http://jsfiddle.net/qeUHd/




回答2:


You can invert the mapping by creating a model object that is mapped internally.

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

Hope this helps.




回答3:


Turns out I have to define all view model properties inside javascript so that knockout can initialize the view model with properties, before I update it with server data

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


来源:https://stackoverflow.com/questions/10906252/knockout-mapping-with-computed-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!