问题
I'm working on a legacy app that I inherited that uses RequireJS and django-require to load many JavaScript modules. I'm somewhat new to RequireJS.
I have a define call in one of the modules
define([
"module",
"lib/jquery",
"./scripts/*myScriptName*",
[...]
], function(module, $, myScriptName, [...]) {
var pr = module.prefix;
*do something with pr*
return myScriptName;
}
"module" is a value of the literal; anything not literal starts with "my"
I suspect "module" here refers to a myScriptName block in a config block in requirejs.config (this config block is a sibling to the paths block and deps block in the config):
{
"config": {
"myScriptName": {
"prefix": "http://myhost.mydomain.tld/path/to/myScriptName",
[...]
},
},
"deps": ["path/to/my/dep"],
"paths": {
"lib": "path/to/my/lib",
"jquery": "path/to/my/lib/jquery"
}
}
Is this a feature of django-require (no tag exists), or of requirejs, and is this a standard block that is documented somewhere? Thanks!
回答1:
RequireJS defines a couple special modules. One of them is named module. It gives access to the current module as an object. For instance, you can export something by setting module.exports to a value. This is just one way to export things from a module. It is possible to get the module name through module.id and its URL through module.uri (yes, that's uri).
There is also a function module.config() that you can use to access the value of the field of the config setting in the object you pass to require.config(). The code currently in the question appears incorrect, it should be:
var pr = module.config().prefix;
The other special module is require, which just gives a reference to the function you can use to load modules.
All of this is stock RequireJS functionality documented in the API.
来源:https://stackoverflow.com/questions/28489524/requirejs-call-to-module