Note that there are some explanatory texts on larger screens.

plurals
  1. POrequire.js - runtime dynamic variables to build path
    primarykey
    data
    text
    <p>Is it possible to inject runtime information in to a require.js "data main" script and use to build paths? More explanation...</p> <p>In my node.js app.js I dynamically find the path to the configured 'theme' like this:</p> <pre><code>var themePath = require('./conf/config.js').config.theme.full_path; </code></pre> <p>and later in the require.js data main script, I'd like to prepend this theme path when defining paths. So assuming I've set my requirejs data-main="xxx" and the following is the xxx file, I'd like to do something like the following:</p> <pre><code>require.config({ baseUrl: "/js/", paths: { "templates" : DYNAMIC_THEME_PATH + '/templates', "views" : DYNAMIC_THEME_PATH + '/views' } }); </code></pre> <p>I'm not sure 1. how I can "see" the themePath from within this require.js data main file, and 2. is this even possible?</p> <p><strong>EDIT - My solution</strong></p> <p>So the real challenge I was having was getting a runtime variable discovered on the server in to the require.js data main script. In node land, global doesn't correspond to the window on the client side (of course) because the javascript isn't in to the browser yet .. duh. So I don't see how you can get this discoverable in the client side script.</p> <p>Ok, so what I WAS able to do was inject the discovered theme path in the ejs, then, dynamically load the data main script with that prepended like:</p> <pre><code>&lt;script data-main="&lt;%= theme_path %&gt;/main" src="../js/libs/require-jquery.js"&gt;&lt;/script&gt; </code></pre> <p>Of course this means I have to have the data main script in the theme directory which wasn't my initial plan; however, it does have the advantage that I can then use relative paths to load my path/to/templates path/to/views, etc. etc.</p> <p>Lastly, I sort of hate when folks answer they're own questions .. so I'm going to leave this up in hopes that someone can either give me a better recommendation or better explain this and they can get the credit ;)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload