Javascript 3仅在一个中起作用

时间:2017-01-10 12:54:56

标签: javascript

我从3个函数(newContractAction,shareGroupsAction,familyAction)中只创建了一个。我使这些功能显示菜单,但我现在只需要制作一个功能。我不是js的进步,所以我需要你们的帮助。

function newContractAction(actions, customerNumber, path) {
  if (actions.indexOf("newcontract") == -1) {
    return null;
  }

  return {
    "index": actions.indexOf("newcontract"),
    "name": "Contract nou",
    "actionLink": SMARTSALES_CONTEXT_PATH + "/buckets.html?customerNo=" + customerNumber + generateReturnUrl(path)
  }
}

function shareGroupsAction(actions, customerNumber, path) {
  if (actions.indexOf("shared") == -1) {
    return null;
  }

  return {
    "index": actions.indexOf("shared"),
    "name": "Configureaza Share Groups",
    "actionLink": SMARTSALES_CONTEXT_PATH + "/orangeShare.html?customerNumber=" + customerNumber + generateReturnUrl(path)
  }
}

function familyAction(actions, customerNumber, path) {
  if (actions.indexOf("family") == -1) {
    return null;
  }

  return {
    "index": actions.indexOf("family"),
    "name": "Configureaza Familie",
    "actionLink": SMARTSALES_CONTEXT_PATH + "/family.html?customerNumber=" + customerNumber + generateReturnUrl(path)
  }
}



this.customActionsCategory = function(actions, customerNumber, path) {

  var customActions = [];
  var newContract = newContractAction(actions, customerNumber, path);
  var sharedGroups = shareGroupsAction(actions, customerNumber, path);
  var family = familyAction(actions, customerNumber, path);

  if (newContract) {
    customActions.splice(newContract.index, 0, newContract);
  }
  if (sharedGroups) {
    customActions.splice(sharedGroups.index, 0, sharedGroups);
  }
  if (family) {
    customActions.splice(family.index, 0, family);
  }

  return customActions;
}

如果action是数组['newcontract','shared','family'],则customerNumber是电话号码,path是文件路径。 我尝试做一个单独的函数并在其中放置一个开关,其中actions.indexOf()就像var for switch一样,但不起作用,因为我必须在同一时间打印出actions数组中的所有项目,因为那些项目是链接菜单中的名称。 有人请帮帮我。

2 个答案:

答案 0 :(得分:0)

通常,您可以添加一种操作,以及包含该类型信息的对象,如下所示:

let types = {
    "newcontract": {
        "title": "Contract nou",
        "link": "/buckets.html?customerNo="
    },
    "shared": {
        "title": "Configureaza Share Groups",
        "link": "/orangeShare.html?customerNumber="
    }
};

function action(type, actions, customerNumber, path) {
    if (actions.indexOf(type) == -1)
        return null;

    return {
        "index": actions.indexOf(type),
        "name": types[type].title,
        "actionLink": SMARTSALES_CONTEXT_PATH + types[type].link + customerNumber + generateReturnUrl(path)
    }
}

注意:

  • 同时拥有customerNocustomerNumber是错误的。选择一个,坚持下去
  • 您可能有错误的方法来构建菜单。它应该比这更简单。

答案 1 :(得分:0)

您可以将if反转并合并到else / if

function action(actions, customerNumber, path){
      if(actions.indexOf("newcontract") !== -1){
        return {
          "index": actions.indexOf("newcontract"),
          "name": "Contract nou",
          "actionLink": SMARTSALES_CONTEXT_PATH + "/buckets.html?customerNo=" + customerNumber + generateReturnUrl(path)
        }
      }else if(actions.indexOf("shared") !== -1){
        return {
          "index": actions.indexOf("shared"),
          "name": "Configureaza Share Groups",
          "actionLink": SMARTSALES_CONTEXT_PATH + "/orangeShare.html?customerNumber=" + customerNumber + generateReturnUrl(path)
        }
      }else if(actions.indexOf("family") !== -1){
        return {
          "index": actions.indexOf("family"),
          "name": "Configureaza Familie",
          "actionLink": SMARTSALES_CONTEXT_PATH + "/family.html?customerNumber=" + customerNumber + generateReturnUrl(path)
        }
      }
    }