0

I have several different tools and each tool has many private and public functions. I use one JSON data file to store settings and data needed for the tools. This because our editors that has no coding knowledge should be able to change the settings and data in the JSON file and adapt all tools to their needs.

All tools are implemented in one module to be able to keep global variables and all tools encapsulated.

I use revealing module for all tools and one more to encapsulate all tools.

Example:
var mainModule = (function () {

    var toolOne = (function () {
        function publicFunc () {
            console.log('Some toolOne action.');
        }
        return {
           publicFunc: publicFunc;
        }
    })();

    var toolTwo = (function () {
        function publicFunc () {
            console.log('Some toolTwo action.');
        }
        return {
           publicFunc: publicFunc;
        }
    })();

    return {
        one: toolOne.publicFunc,
        two: toolTwo.publicFunc
    }
})();

mainModule.one();
mainModule.two();

Is there any better way to organize all tools? What is the best way to load JSON file asynchronously and use settings in all modules?

Mister No
  • 1
  • 1
  • What are these settings and how are they to applied etc? – An0nC0d3r Oct 24 '15 at 21:25
  • 3
    Your code doesn't work. Your inner IIFEs return nothing, so both `moduleOne` and `moduleTwo` are `undefined`. Really, there's no reason to nest the revealing module pattern. – Bergi Oct 24 '15 at 21:28
  • Use a global promise for the loaded JSON. – Bergi Oct 24 '15 at 21:29
  • Your code is not JSON so your question doesn't really make sense. Are you trying to load a file of Javascript code or a file of JSON? If you're using node.js, you can just let `require()` do all the work for you because it will cache the result. Each module can just `require()` the module. The first will load it, the rest will get it from the cache. – jfriend00 Oct 24 '15 at 21:37
  • "Really, there's no reason to nest the revealing module pattern" +1 – An0nC0d3r Oct 24 '15 at 21:46
  • I changed my question and tried to make it more clear. – Mister No Oct 24 '15 at 22:35

1 Answers1

0

Just require the json file at the top of your main module:

var settings = require('./path/to/data.json');
Robbie
  • 18,750
  • 4
  • 41
  • 45