My JS/HTML app is loading some controllers which I have set up as included object literals:
e.g.: page1Controller.js
var page1 = {
initialize() {
$(".page").removeClass('on');
$("#page1").addClass('on');
},
doThis: function() {
// script here
},
doThat: function() {
// Script here
}
}
e.g.: navigation.js
var navigation = {
bindEvents: function() {
var page1Button = document.getElementById('page1Button');
page1Button.addEventListener('click', this.loadPage1, false);
},
loadPage1: function() {
page1.initialize();
},
loadPage2: function() {
page2.initialize();
}
}
note: navigation.js.bindEvents() gets run after pageLoad/deviceLoad event (it's a phonegap app).
and the scripts are included at the bottom of my HTML page as:
<script src="page1Controller.js"></script>
<script src="page2Controller.js"></script>
<script src="page3Controller.js"></script>
<script src="page4Controller.js"></script>
<script src="navigation.js"></script>
It is my understanding that as a default, the scripts will be loaded synchronously - one after the other in the order they appear in the HTML page.
However, when I call say page3.doThat() from navigation.js, the doThat function is undefined, although functions within some of the other controllers work fine. Is it the case that, even though the scripts are loaded in order, navigation.js might be loaded up before page3Controller.js has 'registered' it's functions fully?
navigation.jscould fail to see a filled-inpage1object assuming that's at the top level ofpage1Controller.js.page1Controller.jsand such isn't at the top level? Perhaps it's only run in response to an event, likeonDOMContentReady?