requireJS中的上下文和嵌套模块 [英] Context and nested modules in requireJS

查看:100
本文介绍了requireJS中的上下文和嵌套模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在requireJS中遇到了一些上下文问题。我想要的是在配置阶段(在我加载任何模块之前)创建一个上下文mycontext,然后保持整个上下文。这很复杂,因为我很遗憾需要(< - ha!)为我的模块使用CommonJS语法。所以,如果这是我的基本文件,则如下所示:

I'm having a bit of trouble with contexts in requireJS. What I'd like to is create a context, "mycontext", at the config stage (before I load any modules), and then have that context kept throughout. This is complicated because I am unfortunately required (<- ha!) to use the CommonJS syntax for my modules. So, if this is my base file looks like this:

base.js

contextReq = require.config({
    context: 'mycontext',
    baseUrl: 'http://www.example.com/src/',
    paths:{
        jquery: 'http://ajax.cdnjs.com/ajax/libs/jquery/2.0.3/jquery.min',
    },
});

(function(){
    contextReq(['require', 'topModule'], function(require, topModule){
        topModule.initialize();
    });
})();

然后,我加载topModule:

Then, I load topModule:

http://www.example.com/src/topModule.js

http://www.example.com/src/topModule.js

define(['require', 'jquery', 'nestedModule'], function (require) {
    var $ = require('jquery');
    var other = require('nestedModule');
});

jQuery是否仍然只在mycontext中加载?如果我更进一步怎么办:

Will jQuery still be loaded only in mycontext? What if I go a level further:

http://www.example.com/src/nestedModule.js

http://www.example.com/src/nestedModule.js

define(function (require) {
    var $ = require('jquery');
    var oneMore = require('someOtherModule');
});

在此上下文中我们已经可以访问jquery,但是在此上下文中也会加载someOtherModule ,还是在全球_背景下?在进行require调用之前,有没有办法检查模块是否已经加载?

We already have access to jquery in this context, but will "someOtherModule" also be loaded in this context, or in the global "_" context? Is there any way to check if a module is already loaded before I make the require call?

谢谢!

推荐答案

好的,所以我自己想出来了。要求,在本地或全局,有一个非常有用的属性称为.s,其中列出了所有需求上下文。我的requrie加载完成后,我在控制台上运行了require.s.contexts:

Ok, so I figured this out myself. Require, locally or globally, has a very useful property called ".s" which lists, among other things, all of requires contexts. I ran "require.s.contexts" on to the console after my requrie has finished loading:

base.js

contextReq = require.config({
    context: 'mycontext',
    baseUrl: 'http://www.example.com/src/',
    paths:{
        jquery: 'http://ajax.cdnjs.com/ajax/libs/jquery/2.0.3/jquery.min',
    },
});

(function(){
    contextReq(['require', 'topModule'], function(require, topModule){
        topModule.initialize();
    });
})();

//Timeout, then see where we stand
setTimeout( function () {
    console.log(require.s.contexts._);
    console.log(require.s.contexts.mycontext);
}, 500);

输出如下:

//Console.log for context '_' (the default require context)
{
     [...]
     defined: [], //empty array, nothing has been loaded in the default context
     [...]
}

//Console.log for context 'mycontext' (the default require context)
{
     [...]
     defined: [ //Filled out array; everything is loaded in context!
          topModule: Object
          nestedModule: Object
          jquery: function (e,n){return new x.fn.init(e,n,t)} //jQuery function
     ],
     [...]
}

总而言之,我的预感是正确的:当在特定上下文中加载顶级requireJS模块时,即使不再指定上下文,也会在上下文中加载从该顶级模块中加载的所有模块。

So, in summary, my hunch was correct: when a top level requireJS module is loaded in a particular context, all modules loaded from within that top level module are loaded in context, even if the context is no longer specified.

这篇关于requireJS中的上下文和嵌套模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆