Hello,
I'm a novice at JS, and I'm trying to organise my app with a module kind of system but can't seem to figure something out. In the example below, I define modules in the app.modules object and those get auto run by app.init(). I'm trying to pass app.settings and app.utils to those functions and achieve it in the following code, however the order has to be the same as those that I pass to it. So I couldn't just use utils if I didn't want settings. I'm trying to make them optional, so that if I only need one, I can ask for it and don't get passed the others. Kind of like how you can pass things to angular controllers if and when you need them.
I hope that explains it well enough, I struggled at the title. And the example below is a simplified version, there could be more vars like settings and other objects like app.utils.
Appreciate any advice, thanks!
Code:var app = (function($){ var settings = { option1: 1, option2: 2, etc: 'etc...' } return { init: function(){ for (var prop in this.modules) { if ( this.modules.hasOwnProperty(prop) ) { this.modules[prop].call(null, settings, this.utils); } } } } })(jQuery); app.utils = { getSomething: function(option){ return option; }, getSomethingElse: function(option){ return option; } } app.modules = { runthis: function(settings, utils) { console.log(settings); }, }



Reply With Quote
