使用计算域的剔除映射 [英] knockout mapping with computed fields
问题描述
我从 WCF 服务获取数据,然后映射数据并将数据与我的 DOM 对象绑定:
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);
});
结果在我的 DOM 绑定到模型时显示为例外.我无法找到的是如何添加一些计算出的可观察值,假设我的数据正在返回带有名字和姓氏的人,我如何使用 FN + ' ' + LN 制作计算出的可观察的 FullName.
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.
推荐答案
这是你的 fiddle 的工作副本,我不得不做出很多假设,因为你的 fiddle 甚至不是正确的 javascript 并且看起来很困惑并且没有甚至参考击倒
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));
和一个有效的小提琴:http://jsfiddle.net/qeUHd/
这篇关于使用计算域的剔除映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!