使用敲除绑定从 JS switch case 返回特定值
Returning specific values from JS switch case with knockout bindings
所以我有一个 switch case 函数 returns 多个选项,我试图在各种 knockout 绑定中引用每个选项,但是它不起作用?
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>
解决这个问题的方法是将每种情况下的 return 从
更改为
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>
所以我有一个 switch case 函数 returns 多个选项,我试图在各种 knockout 绑定中引用每个选项,但是它不起作用?
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>
解决这个问题的方法是将每种情况下的 return 从
更改为 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>