RequireJS call to “module”

谁都会走 提交于 2019-12-12 03:25:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!