I am going to maintain someone else's website, and I found a block of code like this (I have removed a lost of code to make example of code simpler)
var Application = (function ($, global) {
'use strict';
global.site = global.site || {};
global.site.App = App;
function App() {
// code removed
}
App.getState = function () {
return 'standard';
};
App.setState = function () {
// Set current state on load
site.App.currentState = site.App.getState();
};
//a lot of code was removed
return {
init:function(){
global.site.app = new global.site.App();
// Set browser sate on load
site.App.setState();
console.log('Application: site.App.currentState', site.App.currentState);
}
}
}(window.jQuery, window));
$(Application.init);
var siteApp = angular.module('siteApp', []);
siteApp.controller('AppController', function() {
console.log('AppController: site.App.currentState', site.App.currentState);
});
The problem is that even though I put var Application = (function ($, global) {...}(window.jQuery, window));
above angularjs module, the angularjs module run first. And I have to admit that I don't fully undenstand
how the var Application ... design pattern work, so my questions are:
- How can I make $(Application.init); run first ?.
- What kind of design pattern is that (Application.init) , so I can googel it and try to understand it
any helps are appreciated.