使用敲除绑定从JS切换案例返回特定值

时间:2018-08-15 13:02:31

标签: javascript switch-statement knockout-2.0

所以我有一个switch case函数,它返回多个选项,我试图在各种敲除绑定中引用每个选项,但是它不起作用?

function messageIcon (type) {
    return ko.computed(function () {
        switch (type) {
            case InteractionMessageTypes.Customer:
                return {
                    tooltip: "This message was sent by the customer",
                    icon: "icon icon-user4",
                    contentClass: "customer-message"
                };
            case InteractionMessageTypes.Agent:
                return {
                    tooltip: "This message was sent by an agent",
                    icon: "icon icon-headset",
                    contentClass: "agent-message"
                };
            case InteractionMessageTypes.Initiate:
                return {
                    tooltip: "This session has started",
                    icon: "icon icon-phone2",
                    contentClass: "system-message"
                };

<div data-bind="css: messageIcon($data.contentClass)">
  <span data-bind="css: messageIcon($data.icon), attr:{title: messageIcon($data.tooltip)}"></span>
</div>

1 个答案:

答案 0 :(得分:0)

解决此问题的方法是将每种情况下的收益都更改为

 return {
                    tooltip: "This message was sent by the customer",
                    icon: "icon icon-user4",
                    contentClass: "customer-message"
                };

return '<span class="icon icon-user customer-message" title="This message was sent by the customer">' + '</span>';
    }

然后像这样将数据绑定html

 <span data-bind="html: messageIcon($data.Type)">

                </span>