如何从knockout data-bind引用typescript的枚举值?

时间:2016-08-26 08:22:52

标签: knockout.js typescript enums

我正在努力完成这项工作(页面抱怨客户体验未定义):

<div data-bind="visible: $data.customerExperienceObservable() === CustomerExperience.Loading">
    Loading...
</div>

其中CustomerExperience在TypeScript中定义:

export enum CustomerExperience {
    Loading = 1,
    Unconfigured = 2,
    Data = 3
}

转化为:

(function (CustomerExperience) {
    CustomerExperience[CustomerExperience["Loading"] = 1] = "Loading";
    CustomerExperience[CustomerExperience["Unconfigured"] = 2] = "Unconfigured";
    CustomerExperience[CustomerExperience["Data"] = 3] = "Data";
})(exports.CustomerExperience || (exports.CustomerExperience = {}));
var CustomerExperience = exports.CustomerExperience;

将上下文应用于一个类的实例:

ko.applyBindings(dataProvider);

我知道如果我将CustomerExperience定义为绑定dataProvider的属性,我可以使它工作。但我试图了解是否如何使用TypeScript枚举。

有什么想法吗? :)

PS:现在我使用=== 1来使它工作。

1 个答案:

答案 0 :(得分:1)

结束这个解决方案(谢谢Carrie Kendall!):

public customerExperienceLoading: KnockoutComputed<boolean> = ko.computed({
    owner: this,
    read:  () => {
        return this.customerExperienceObservable() === CustomerExperience.Loading; } });

并将其绑定为常规布尔值:

data-bind="visible: $data.customerExperienceLoading() === true"