load modules using module name and not the file name

541 Views Asked by At

I have two 'define' in two separate js files.

def1.js and def2.js

define("mydefname1",["file1",...]});

define("mydefname2",["file2",....]});

I have another require statement where i check if the two definetions are loaded.

require(['def1','def2'], function(){alert('loaded')});

this works fine..

but if I try

require(['mydefname1','mydefname2'], function(){alert('loaded')});, 

it does not work.

Is there a way I could actually use mydefname1 and mydefname2.. i.e. the module name to load them, and not the file name?

2

There are 2 best solutions below

0
On

As far as I know, that's not possible. What I usually do is format my file names to include my module name, so I can include it that way.

For a metaclass name Overlay, the file name would be class.Overlay.js and my require/include functions would take my module name and build the file name from it.

0
On

As I understand it, RequireJS recommends that you avoid manually assigning module names - see here: http://requirejs.org/docs/api.html#modulename

FWIW, have you played around with the "path" configuration option? I don't think you can use it to do exactly what you are looking to do but it may offer an acceptable alternate approach. See here: http://requirejs.org/docs/api.html#config

Hope it helps!