我有以下代码:
var btns1 = {
'Submit': function (win) {
submitHandler(oLink.$link, $('#main-form'), false);
},
'Submit & Close': function (win) {
submitHandler(oLink.$link, $('#main-form'), true);
},
'Close': function (win) {
modal.closeModal()
}
}
var btns2 = {
'Submit & Close': function (win) {
submitHandler(oLink.$link, $('#main-form'), true);
},
'Close': function (win) {
modal.closeModal()
}
}
if (oLink.title.substr(0, 4) == "Crea") {
if (content.match(/data-RowKey="(.{3}).*/)) {
oLink.title += " " + content.match(/data-RowKey="(.{3}).*/)[1]
}
var btns = btns1;
}
if (oLink.title.substr(0, 4) == "Edit") {
var btns = btns1;
}
if (oLink.title.substr(0, 4) == "Dele") {
var btns = btns2;
}
有没有办法可以重构代码。我在想的是把它放到一个名为“adminModalBtns”的函数中,让它以 oLink 和 content 作为参数并让它返回 btns。用 if-else 或 case 语句来做这件事最清楚吗?