I have the code below. It looks long and complicated and now I have to add some more conditions. Is there any way I can simplify this code. All I can think of is combining the City and Menu switches but then if I do that I have to do get the pk value one way for City and another way for Menu. Does javascript offer any other way to do the same thing as a switch? Sorry for the long code listing but I thought I should include everything.
function getParams(entity) {
var store = window.localStorage;
var table = "Content";
switch (entity) {
case "City":
if (store.getItem('AccountID')) {
pk = store.getItem('AccountID') + "04" + "000";
return {
pk: pk,
param: '?pk=' + pk,
table: table,
rc: true
}
} else {
paramOnFailure("Please reselect Account");
return { rc: false }
}
break;
case "Menu":
if (store.getItem('AccountID')) {
pk = store.getItem('AccountID') + "00" + "000";
return {
pk: pk,
param: '?pk=' + pk,
table: table,
rc: true
}
} else {
paramOnFailure("Please reselect Account");
return { rc: false }
}
break;
case "Page":
if (store.getItem('AccountID') && store.getItem('PageID')) {
pk = store.getItem('AccountID') + store.getItem('PageID') + "000";
return {
pk: pk,
param: '?pk=' + pk,
table: table,
rc: true
}
} else {
paramOnFailure("Please reselect Account and Page Type");
return { rc: false }
}
break;
case "Question":
if (store.getItem('AccountID') &&
store.getItem('CityID') &&
store.getItem('TopicID') ) {
pk = store.getItem('AccountID') + store.getItem('CityID');
return {
pk: pk,
param: '?pk=' + pk + '&rk=' + store.getItem('TopicID'),
table: "Question",
rc: true
}
} else {
paramOnFailure("Please reselect Account, City and Topic");
return { rc: false }
}
break;
case "Reference":
if (store.getItem('ReferenceID')) {
pk = store.getItem('ReferenceID');
return {
pk: pk,
param: '?pk=' + pk,
table: "Reference",
rc: true
}
} else {
paramOnFailure("Please reselect Reference");
return { rc: false }
}
break;
case "Topic":
if (store.getItem('AccountID') && store.getItem('CityID')) {
pk = store.getItem('AccountID') + "05" + store.getItem('CityID');
return {
pk: pk,
param: parameters = '?pk=' + pk,
table: table,
rc: true
}
} else {
paramOnFailure("Please reselect Account and City");
return { rc: false }
}
break;
default:
;
}
Put this part of you code in function and call it with the proper parameter
So you code will be somthing like this and call this function from each Switch Case