Search code examples
visual-studiocordovamulti-device-hybrid-apps

How to define global variables, based on config (debug/release)?


How to define global variables for Multi-Device Hybrid Apps, based on config (debug/release/distribution)?

Example: my app have to query (HTTP POST, using ajax) web server. When debugging, i wish to query local web server, when building release config, i wish to query production web server.

UPD: Avani adviced to use Cordova Hooks, but: 1) looks like hooks not implemented in MDHA (link), and 2) if implemented, where to store hooks? \project_dir\hooks or \project_dir\bld\Debug\hooks, or some other place or method? MDHA docs are missing.

I tried to add hook to \project_dir\hooks\after_prepare (it modifies index.html), and got compilation error:

EPERM, operation not permitted 'C:\Temp\BlankCordovaApp1\BlankCordovaApp1\bld\Debug\_www'

Solution

  • Since configuration is a build-time property, the only way to get its value in to your source code is to generate the source code at build-time. This means creating a template in a templating language like T4, and then hacking your project file to expand the template and generate the desired source code. I recommend the Clarius.TransformOnBuild package for triggering code generation at build-time.

    Compile-time directives like #if do not exist in Javascript or HTML.