我正在尝试构建一个针对用户一周计划的应用程序,一切正常,除了一件事,当用户输入重复的时间时出现错误,但我的问题是应用程序仍在运行该计划,显示于用户界面。
所以我尝试使用return
关键字来阻止该应用程序继续运行,但这并不能解决问题,所以这是我的代码:
JavaScript:
var internalController = (function(UICtrl) {
var Plan = function(id, from, to, text, goingToCkecked) {
this.id = id;
this.from = from;
this.to = to;
this.text = text;
this.goingToCkecked = goingToCkecked;
};
var data = {
Monday: [],
Tuesday: [],
Wednesday: [],
Thursday: [],
Friday: [],
Saturday: [],
Sunday: []
};
//the array of the from inputs
var fromT = [];
//the array of the to inputs
var toT = [];
var Dom = UICtrl.getDOMstrings();
function removeError(x, y) {
document.querySelector(x).style.visibility = "hidden";
document.querySelector(y).classList.remove("error-red");
}
function showER() {
document.querySelector(Dom.errorCase).style.visibility = "visible";
document.getElementsByClassName(Dom.errorDes)[0].innerHTML = "the \" From time \" is already chosen";
document.querySelector(Dom.inputTimeF).classList.add("error-red");
}
var exit;
return {
refuseDuplicatedPlans: function(from, to) {
var exit;
if (fromT.indexOf(from) === -1) {
fromT.push(from);
exit = false;
} else {
console.log('value already exist');
showER();
exit = true;
}
}
};
})(UIController);
var controller = (function(interCtrl, UICtrl) {
var input, newPlan, DOM;
DOM = UICtrl.getDOMstrings();
function setupEventListeners() {
document.querySelector(DOM.inputBtn).addEventListener("click", ctrlAddPlans);
document.addEventListener("keypress", function(e) {
if (e.keyCode === 13) {
document.activeElement.blur();
ctrlAddPlans();
}
});
}
function removeFocus() {
console.log('remove Focus got trigered');
document.querySelector(DOM.inputTimeF, DOM.inputTimeT, DOM.inputText).addEventListener("focus", function() {
// document.activeElement.blur();
console.log("hello world");
});
}
var ctrlAddPlans = function() {
//3.get the filed input data
input = UICtrl.getInput();
console.log(input);
// 4.Refuse duplicated plans
interCtrl.refuseDuplicatedPlans(input.inputTimeF, input.inputTimeT);
//5.add the plan to the internalController
newPlan = interCtrl.addItem(input.inputDay, input.inputTimeF, input.inputTimeT, input.inputText, input.goingToCkecked);
//6.add the plan to the UI
UICtrl.addPlanList(newPlan, input.inputDay);
//7.clear the fields;
interCtrl.clearFields(UICtrl.getDOMstrings().inputTimeF, UICtrl.getDOMstrings().inputTimeT, UICtrl.getDOMstrings().inputText);
};
return {
init: function() {
console.log('the app has started');
setupEventListeners();
},
};
})(internalController, UIController);
controller.init();
如果它返回true,我当然希望短路发生在refuseDuplicatedPlans(
)方法之后的控制器模块中,我希望模块停止其执行,并感谢您。
答案 0 :(得分:1)
您的rejectDuplicatedPlans()方法现在不返回任何内容。请向其中添加一个返回声明
refuseDuplicatedPlans: function(from, to) {
var exit;
if (fromT.indexOf(from) === -1) {
fromT.push(from);
exit=false;
} else {
console.log('value already exist');
showER();
exit=true;
}
return exit;
}
之后,您可以放置if else调用,如果为true,则停止执行。
if(interCtrl.refuseDuplicatedPlans(input.inputTimeF, input.inputTimeT)){
reutrn;
}