为了编写一些干净整洁的JavaScript,我正在尝试使用Modules。我创建了这个规则模块:
//RulesModule.js
var RulesModule = (function () {
return {
checkModal: function () {
var checkModalContents = // string contents
return checkModalContents;
},
ruleModals: function (modalType) {
switch (modalType) {
case "CheckDateRule":
return checkModal();
default:
return "UNKNOWN MODAL";
}
}
}
});
然后我挂钩到下拉元素的事件,当选择一个选项时,我们将该选项传递给RulesModule.ruleModals方法,以获得正确的模态。
//EventSubscriptions.js
var go = false;//switch off
$('#rules').on('click', function () {//on click
if (go) {//if go
var selectedRule = $("#rules").find(":selected").val();
var contents = RulesModule.ruleModals(selectedRule); // this line throws the error
console.log(contents);
$("#modalContents").empty(); // clear up contents
$("#modalContents").append(contents);
$("#myModal").css("display", "block");
go = false;//switch off
} else { go = true; }//if !go, switch on
}).on('blur', function () { go = false; });//switch off on blur
在我的HTML中,我在标题中加载了两个javascript文件:
<head>
<meta charset="utf-8" />
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="~/JavaScript/Modules/RulesModule.js"></script>
<script src="~/JavaScript/EventSubscriptions.js"></script>
</head>
答案 0 :(得分:1)
RulesModule
的回复是一个对象checkModal
&amp; ruleModals
是两种方法,因此请尝试按checkModal
this.checkModal
var RulesModule = (function() {
return {
checkModal: function() {
var checkModalContents = ''; // string contents
console.log('Check Modal called')
return checkModalContents;
},
ruleModals: function(modalType) {
switch (modalType) {
case "CheckDateRule":
return this.checkModal();
default:
return "UNKNOWN MODAL";
}
}
}
}());
console.log(RulesModule.ruleModals('CheckDateRule'))
&#13;