简体   繁体   English

ko.observableArray和JSON数据按摩

[英]ko.observableArray and JSON data massaging

I'm using web services to load data to client side. 我正在使用Web服务将数据加载到客户端。 For binding purposes I need to expand on data that I get. 为了进行绑定,我需要扩展获得的数据。 Ie I don't want to massage all data on server side. 即我不想在服务器端处理所有数据。

For example, object Trip { Id: "123", Status: "P" } 例如,对象Trip { Id: "123", Status: "P" }

In HTML I bind table to observableArray and want to display "Pending" instead of "P". 在HTML中,我将table绑定到observableArray,并希望显示“ Pending”而不是“ P”。 I'm coming from Silverlight/MVVM and usually you would use converter or just add new R/O property to object. 我来自Silverlight / MVVM,通常您会使用转换器,或者只是向对象添加新的R / O属性。

Not sure how this scenario should be handled in knockout.js 不知道该如何在敲门js中处理

You may find here all you need : 您可能会在这里找到所需的一切:

http://net.tutsplus.com/sessions/knockout-succinctly/ http://net.tutsplus.com/sessions/knockout-succinctly/

Have a good read. 好好阅读。

If you are just looking for a converter, computed observables are a good candidate. 如果您只是在寻找转换器,则可computed观测值是一个不错的选择。

var Tip = function(data) {
    var self = this;
    self.id = data.id;
    self.status = ko.observable(data.status);

    //You may prefer fullStatus, or statusName
    self.statusConverter = ko.computed(function() { 
        return self.statusMap[self.status()];
    });
};

Tip.prototype.statusMap = {
    P: "Pending",
    O: "Open",
    C: "Closed"
};

which you can bind to like this: 您可以像这样绑定:

<td data-bind="text: statusConverter"></td>

You can see it in this fiddle 你可以在这个小提琴中看到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM