Is there a way to set baseurl dynamically in require.js?

I am creating a Pi-engine application (php application engine based on zend framework 2).

In my application, the path to the javascript file depends on the name of the application, and the name of the application is extracted by the installer.

For example, url require.js would be:

http://my.site.name/asset/module-{module_name}/script/js/require.js

module_name changes depending on which name is specified by the application.

I know that I can put the path in the 'data-main' attribute in the backend, for example:

<script data-main="/asset/module-{module_name}/script/" src="/asset/module-{module_name}/script/js/require.js"></script>

But I want to know if there is a way to set baseurl dynamically using javascript so that I don't need to touch the backend.

+5
source share
4 answers

http://requirejs.org/docs/api.html#config

, , require.js, . , require.js require():

<script>
    var require = {
        baseUrl: generateBaseUrl()
    };
</script>
<script src="scripts/require.js"></script>
+10

, "baseUrl" , , js.

require.config({
   baseUrl: "/js/"
});
0

, javascript : javascript

var scripts = document.getElementsByTagName("script");

require.config({
    baseUrl: '/asset' + scripts[scripts.length-1].src.match(/\/module.*?\//)[0] + 'script/',
}); 

, .

0

, , , :

require.config((function(){
    var scripts = document.getElementsByTagName("script");
    return {
        baseUrl: '/asset' + scripts[scripts.length-1].src.match(/\/module.*?\//)[0]
    };
})());
0

All Articles