Search code examples
javascriptrequirejsamd

RequireJS, AccountingJs - passing global config into accounting


Im quite new to using requireJs and im having an issue with setting global configuration for a module.

I am using accountingJs and want to modify the setting globally in this case i want to change the symbol from $ to £.

Without RequireJS you would simply do something like this as accounting would be in the global namespace

accounting.settings = $.extend(accounting.settings, {
        currency: { symbol: '\u00A3 '}
    });

accountingJs is AMD compliant and works perfectly with require but i cant seem to figure out a way of passing the config into it globally rather than .

I have seen the config setting in require docs here and i can set the config here but accountingjs doesn't pick this up (it isnt coded to!).

My question is how can i set configuration like this for a AMD compliant module globally within the page?

I can see a few options

  • Edit accountingjs to look at module.config() and load any config it sees - i have tried this and it does work as expected but i dont really want a custom.
  • use shim config and use the init call back - i havent got this to work (maybe because it is already AMD compliant)
  • create a new module to wrap accountingjs in another define and apply the config here and use this module in each page - not tries this but i guess it would work...

what i really ant to do is have a way of globally applying config to an already existing module from the require config is it possible??


Solution

  • If the AMD module is not designed to use module.config, then you can't force it to use it. The solution you mention last is the most robust: create a wrapper module that configures the actual module as you want. This wrapper can use module.config to grab values. This solution is likely to work with RequireJS now and for quite a long time since you're using API features that are well documented and central to RequireJS' functionality.

    As for a shim, I don't recall the docs for RequireJS ever providing a solution that consists as using a shim for a module that is already designed to work with AMD loaders. So if using a shim worked, it would be by happenstance rather than by design.