Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequireJS and common config

Is possible to have requirejs config at one place and reuse it in modules?

such as

main.js:

requirejs.config({
    baseUrl: "static/js", 
    paths: {
         "jquery": "http://code.jquery.com/jquery-1.9.1.js",    
     "jquery-ui": "http://code.jquery.com/ui/1.10.2/jquery-ui.js"
},
shim: {
     "jquery-ui": {
          deps: ["jquery"]
     }
}
}); 

and

public.js:

define(["main", "jquery", function(main, $) {
    // do some public stuff
});

client.js:

define(["main", "jquery", function(main, $) {
    // do some client stuff
});

And on my public part of web have

<script type="..." src="js/require.js" data-main="js/public.js"></script>

And on client part of web

<script type="..." src="js/require.js" data-main="js/client.js"></script>

And also I would like to have a module for each page. So for example to have a index module on public

<script ...>
    require('public/index');
</script>

and

public/index.js:

define(["jquery", "slideshow"], function($, s) {
    $( function() { s.init() } );
});

Is that possible with RequireJS?

Thank you for answers.

like image 603
Douglish Avatar asked Feb 17 '23 19:02

Douglish


1 Answers

data-main is a useful shortcut in the very simple case but I don't find it terribly useful beyond that, the solution is to dump it entirely.

Load your main explicitly on each page, then use the callback to load your view-specific scripts.

So you have in public.html:

<script src="/Scripts/require.js"></script>
<script>
require('main', function(){
  require('public');
})
</script>

and in client.html:

<script src="/Scripts/require.js"></script>
<script>
require('main', function(){
  require('client');
})
</script>

I've written a blog post expounding on this idea here.

like image 149
George Mauer Avatar answered Mar 04 '23 20:03

George Mauer