Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding paths to RequireJS configuration on runtime

Ok, I already know that you should configure paths with RequireJS like this

require.config({
  paths: {
    name: 'value'
  }
});

And call it like this.

require(['name'], function() {
    /* loaded */
});

But the thing is, I'm working in environment in which I don't have access to the existing call to require.config(...). For those who care, the environment is Azure Mobile Services scheduled job. Microsoft has already included RequireJS in the environment and configured the paths. My question is two-fold.

1. How do I add paths to the existing require.config()? I know calling require.config() again will destroy the existing configuration. Which is what I do not want to do.

2. How do I get to know which paths have already been configured? I really wouldn't like to overwrite any existing path name or overwrite any existing library by accident.

like image 556
Jani Hyytiäinen Avatar asked Aug 25 '13 09:08

Jani Hyytiäinen


People also ask

Is RequireJS still relevant?

RequireJS has been a hugely influential and important tool in the JavaScript world. It's still used in many solid, well-written projects today.

How do I call a function in RequireJS?

2 Answers. Show activity on this post. require(["jquery"], function ($) { function doStuff(a, b) { //does some stuff } $('#the-button'). click(function() { doStuff(4, 2); }); });

How add RequireJS to HTML?

To include the Require. js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module. This can be taken as the main entry point to your application.

What is RequireJS config?

Advertisements. RequireJS can be initialized by passing the main configuration in the HTML template through the data-main attribute. It is used by RequireJS to know which module to load in your application. For instance − <script data-main = "scripts/main" src = "scripts/require.js"></script>


1 Answers

Running require.config() again does not override your original config file. It actually extends it and adds your new paths to it. Right now I am using it this way, where configfile is also a require.config({})

<script data-main="configfile" src="require.js"></script>
<script>
    require.config({
        paths: {
            prefix-name: 'path/to/file'
        }
    });
</script>

One way to avoid name collisions with Azure Mobile paths would be to simply prefix all your custom paths.

Disclaimer: I have never used Azure Mobile, just RequireJs. You may have to implement it a little differently but it is possible.

like image 110
Matt Derrick Avatar answered Oct 18 '22 09:10

Matt Derrick