在 dynamics 365 中简化 JavaScript

Simplify JavaScript in dynamics365

我在 js 中有一个函数,里面有两个条件检查,我该如何简化它。我在 d365

中使用这个

看来我写的代码比较复杂,你能帮忙简化一下吗

function selectedNeedSupport(executionContext) {
  var formContext = executionContext.getFormContext();
  let selectedItem = formContext.getAttribute("neededsupport").getValue();
  if (selectedItem != null && selectedItem.includes(9)) {
    formContext.getControl("comments").setVisible(true);
    if (selectedItem != null && selectedItem.includes(8)) {
      formContext.ui.tabs.get("tab_Application").setVisible(true);
    } else {
      formContext.ui.tabs.get("tab_Application").setVisible(false);
    }
  } else {
    formContext.getControl("comments").setVisible(false);
    if (selectedItem != null && selectedItem.includes(8)) {
      formContext.ui.tabs.get("tab_Application").setVisible(true);
    } else {
      formContext.ui.tabs.get("tab_Application").setVisible(false);
    }
  }
}

您有测试和布尔值。使用测试而不是 true 和 false:

const nine = selectedItem?.includes(9)    
const eight = selectedItem?.includes(8)

formContext.getControl("comments").setVisible(nine && !eight);

例如

function selectedNeedSupport(executionContext) {
  const selectedItem = formContext.getAttribute("neededsupport").getValue();
  const formContext = executionContext.getFormContext();
  formContext.getControl("comments").setVisible(selectedItem?.includes(9));
  formContext.ui.tabs.get("tab_Application").setVisible(selectedItem?.includes(8));  
}