app.js
Common.js
HomeController.js
window.App = { | |
common: require('./app/Common'), | |
HomeController: require('./app/Controllers/HomeController'), | |
}; | |
var UTIL = { | |
exec: function (controller, action) { | |
var ns = App, // Change the namespace | |
action = (action === undefined) ? "init" : action; | |
if (controller !== '' && ns[controller] && typeof ns[controller][action] == 'function') { | |
ns[controller][action](); | |
} | |
}, | |
init: function() { | |
var body = document.body, | |
controller = body.getAttribute('data-controller'), | |
action = body.getAttribute('data-action'); | |
UTIL.exec('common'); | |
UTIL.exec(controller); | |
UTIL.exec(controller, action); | |
UTIL.exec('common', 'finalize'); | |
} | |
}; | |
$(document).ready(UTIL.init); |
(function() { | |
'use strict' | |
// Constructor | |
function Common() { | |
// Example of calling to a private method from the constructor | |
_foo(); | |
} | |
// Public methods | |
Common.prototype.init = function() { | |
console.log('App.common.init()'); | |
}; | |
Common.prototype.finalize = function() { | |
console.log('App.common.finalize()'); | |
}; | |
// Private methods | |
function _foo() { | |
} | |
// Export | |
module.exports = new Common; | |
})(); |
(function() { | |
'use strict'; | |
// Constructor | |
function HomeController() { | |
} | |
// Public methods | |
HomeController.prototype.init = function() { | |
console.log('HomeController.common.init()'); | |
}; | |
HomeController.prototype.index = function() { | |
console.log('HomeController.common.index()'); | |
}; | |
HomeController.prototype.delete = function() { | |
console.log('HomeController.common.delete()'); | |
}; | |
// Private methods | |
// Export | |
module.exports = new HomeController; | |
})(); |