2013-11-26 31 views
1

我是新来的淘汰赛,我有以下问题:淘汰赛depandent变量:如何定义结构正确

我从IDS数据库来了,每个ID都有其相应的说明(这是.NET actualy枚举,但我认为这个问题在这个问题上并不重要)。

例如,

a)用于变量 “PType ”:0 - Undefined; 1 - Low Structure; 2 - Hight Structure b)中为变量“ ClientType”:0 - Undefined, 1 - P Type; 2 - S Type 等某些其他变量也

如何正确地定义模型这种依赖性? 目前我只喜欢

PType: ko.observable(); 
ClientType: ko.observable(); 

ID和我出示本人身份证件页:

<span data-bind="text: PType"></span> 
<span data-bind="text: ClientType"></span> 

不过,我需要这样的:PTypeDescriptionClientTypeDescription来显示用户。我相信这些都是某种因变量,但无法让它工作。

回答

1

首先我会假设你已经知道什么枚举你有,当你通过AJAX获取数据,你就会得到枚举值表示为integerstring

您可以模拟枚举JavaScript简单(检查this article):

var PType = { 0: "Undefined", 1: "Low Structure", 2: "Hight Structure" } 
var ClientType = { 0: "Undefined", 1: "P Type", 2: "S Type" } 

所以,你的视图模型可以是这样的:

var itemObj = { 
    PType: ko.observable(0); 
    ClientType: ko.observable(0); 
    property1:ko.observable('')// put here the other properties if you have more 
} 

为了让您的枚举重作为enum编写的调用函数,它使用value(“你的枚举键”)和哪个枚举使用(“你可以使用内联函数”)。

JsFiddle Demo

更新
检查这个SO answer在JS另一个implementaion的枚举,它的简单而有效的