I\'m new to RequireJS. I\'m writing a number of custom bindings in Knockout.js and want to split them up using modules.
The layout of my code at the moment is:
Relative IDs are resolved relative to the module ID within which the ID is resolved. See AMD spec's module id format section.
There are two ways to frame a relative dependency ID into a correct context/scope:
Define call is the start/definition of "module." All dependencies asked for within define() call are scoped to be within/relative to that module's ID. Example:
// does not matter what the file name is.
define(
'hand/named/module'
, ['./child']
, factoryFunction
)
or
// inside of 'hand/named/module.js' file.
define(
['./child']
, factoryFunction
)
In both of the above cases, ./child is resolved against the module ID defined by the define() call. The module id in both cases is hand/named/module and the ./child is resolved to hand/named/child (+ '.js' obviously, when time comes to get it)
You can change the scope of require call from global to local by overriding it. You actually don't need to override / keep the name require, it's the meaning of what it does changes. The require functionality becomes "local" to a particular module.
// inside 'hand/named/module.js' file
define(
['require']
, function(myLocalRequire){
require('./child', function(){
// outcome A
})
myLocalRequire('./child', function(){
// outcome B
})
}
)
There in outcome A you continue to use "global" require - the one attached to parent scope. Your ./child resolves to baseURL + '/child'
The outcome B is locally-scoped, tied to module id hand/named/module so, ./child is resolved to hand/named/child
What @CristiPufu recommended is to override the global require variable with local object that will be local only to the scope of that function:
// inside 'hand/named/module.js' file
define(
['require']
, function(require){
return function(){
// here we have access only to "local" require,
// since in the function declaration you decided to
// override the 'require' variable with new object.
// All code outside of this function will use global require.
require('./child', function(){
// outcome B
})
}
}
)
My preference is to put all relative resources inside define call. Makes them explicit and meeningfull as it's clear what they are relative to.