最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - requireJS - a few questions - Stack Overflow

programmeradmin1浏览0评论

sorry for beeing a little lazy and not trying it all out myself, but I thought a nice answer on Stackoverflow might help some other guys too. I'm pondering whether or not to use requireJS to load my modules. Currently I'm doing that on my very own, so I have some questions about requireJS.

  • How does requireJS deal with multiple references (does it cache files/modules) ?

More precisely, if you have calls like require(["some/module", "a.js", "b.js"], function...}); and you again reference a.js or b.js in later .require or .define calls, how does requireJS handle those? My guess is, it will entirely ignore those additional references, is that correct? If so, is it possible to force requireJS to reload a script ?

  • Does requireJS always transfer files over the wire or you can load modules statically ?

What I normally do is, to concatenate all of my js files (modules included), except for those which need to get loaded dependent on run-time conditions. As far as I read the requireJS doc, you can define own names for modules. So my question is, can you load a module which is already present in the script, without transferring it over the wire ? As far as I understood the doc, names are created automatically for modules, based on their path location and filename, so this would make no sense for my requirement here.

sorry for beeing a little lazy and not trying it all out myself, but I thought a nice answer on Stackoverflow might help some other guys too. I'm pondering whether or not to use requireJS to load my modules. Currently I'm doing that on my very own, so I have some questions about requireJS.

  • How does requireJS deal with multiple references (does it cache files/modules) ?

More precisely, if you have calls like require(["some/module", "a.js", "b.js"], function...}); and you again reference a.js or b.js in later .require or .define calls, how does requireJS handle those? My guess is, it will entirely ignore those additional references, is that correct? If so, is it possible to force requireJS to reload a script ?

  • Does requireJS always transfer files over the wire or you can load modules statically ?

What I normally do is, to concatenate all of my js files (modules included), except for those which need to get loaded dependent on run-time conditions. As far as I read the requireJS doc, you can define own names for modules. So my question is, can you load a module which is already present in the script, without transferring it over the wire ? As far as I understood the doc, names are created automatically for modules, based on their path location and filename, so this would make no sense for my requirement here.

Share Improve this question asked Apr 17, 2012 at 16:08 jAndyjAndy 236k57 gold badges313 silver badges363 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

requirejs.undef() should do the trick

Normally, a module will only be loaded once by require.js. require.js will always resolve dependencies and load the modules in the right order so that you don't have to care about that. Subsequent calls to require for the same module will yield it immediately.

It is not possible to reload a module. If you really have a need for loading the same module more than once (which would unfortunately indicate that there is something wrong with your module's design) you can have a look at the Multiversion support.

I am not sure I understand what you mean by "load modules statically". But if I am guessing right you want to load several modules as one and use them seperately. This is possible: Typically in your modules you will be doing something like:

define(['moduleA', 'moduleB', 'moduleC'], function (a, b, c) {
    ...
    return exports;
});

where exports can be more or less anything, a function, an object, whatever. So you can also do something like:

define(['moduleA', 'moduleB', 'moduleC'], function (a, b, c) {
    ...
    return {moduleA: a, moduleB: b, moduleC: c};
});

exporting them all together.

Note however that you should really have a look at the optimization tool. It can group related modules together.

Finally, the automatic naming is a misunderstanding, you can be explicit on the names of your modules.

发布评论

评论列表(0)

  1. 暂无评论