简体   繁体   English

何时定义模块以及何时仅需要使用requireJS的文件

[英]when to define a module and when to only require files using requireJS

I'm struggling to get requireJS to work properly. 我正在努力让requireJS正常工作。 Page is running fine, but I think I'm doing things in an oh-so wrong way. 页面运行正常,但我认为我正在以一种错误的方式做事。

For example, on page xzy I'm adding the following JavaScript at the end of the page (the JS must stay on the page for now, so no external js-files possible) 例如, 在页面xzy上在页面末尾添加了以下JavaScript(JS现在必须保留在页面上,因此不能使用外部js文件)

<script type="text/javascript" language="javascript">
  //<![CDATA[
  (function () {
    require([
      'async!http://maps.google.com/maps/api/js?v=3&sensor=false',
      'maps/jquery.ui.map.full.min.js',
      'maps/jquery.ui.map.extensions.min'
      ], function() {

        // ... do stuff with Google Maps

      }
    );
  }());
//]]>
</script>

Doing this makes google.map and the $.().gmap method globally available, which probably shouldn't be available globally. 这样做会使google.map$.().gmap方法全局可用,这可能不应该全局可用。

Questions: 问题:
Should I convert this into a requireJS module? 我应该将其转换为requireJS模块吗? Why? 为什么?

If so, will the module be available on other pages as well or do I just "re-define" on page 123 and the dependency files will already have been cached? 如果是这样,该模块是否也可以在其他页面上使用,或者我只是在第123页 “重新定义”并且依赖文件已经被缓存了?

And finally - will I have to convert the code inside my require call into module.methods, which I then call via module_name.method_name(pass_some_parameters) ? 最后 - 我是否必须将require调用中的代码转换为module.methods,然后通过module_name.method_name(pass_some_parameters)调用?

Just looking at the JS: 只看JS:

http://maps.google.com/maps/api/js?v=3&sensor=false http://maps.google.com/maps/api/js?v=3&sensor=false

You can see that window.google is a global. 您可以看到window.google是全局的。 There's not much you can do about that without Google releasing an AMD version. 如果没有Google发布AMD版本,你就无法做到这一点。

Your decision regarding should you create a module should firstly be a question of readability/maintainability of the JS code. 您是否应该创建模块的决定首先应该是JS代码的可读性/可维护性问题。 Modules are (should be), readable, reusable chunks of code/reusable abstractions that the rest of your code can consume. 模块是(应该是)可读的,可重用的代码/可重用抽象块,其余代码可以使用这些抽象。 You should also derive testing benefits from this - each module should be easier to test in isolation. 您还应该从中获得测试的好处 - 每个模块应该更容易单独测试。

You can end up with many more JS files if you choose a modular approach, and you might think this leads to performance issues - ie multiple HTTP requests. 如果选择模块化方法,最终可能会有更多JS文件,并且您可能认为这会导致性能问题 - 即多个HTTP请求。 But this is mitigated by using the RequireJS Optimiser to optimise your modules to a single file. 但是,使用RequireJS Optimiser将模块优化为单个文件可以减轻这种影响。

If you convert to a module, yes you can require it from other pages, and if your HTTP caching headers are set up, then the browser may choose to use a cached version, thus saving you a HTTP request (same caching heuristics apply if you've optimised every module into a single file). 如果您转换为模块,是的,您可以从其他页面require它,如果您的HTTP缓存标头已设置,则浏览器可以选择使用缓存版本,从而为您保存HTTP请求(如果您同样适用缓存启发式)已将每个模块优化为单个文件。

If you re-define (I assume you mean copy and paste the code block), then those dependencies listed in the call to require should all be cached by the browser, and therefore instantly available (depending on your web server and its HTTP caching headers). 如果你重新定义 (我假设你的意思是复制并粘贴代码块),那么在require的调用中列出的那些依赖项应该全部由浏览器缓存,因此立即可用(取决于你的web服务器及其HTTP缓存头) )。

Finally, yes you may have to refactor the code a bit to expose the new module's API. 最后,是的,您可能需要重构代码以暴露新模块的API。 If that means exposing a single object with methods, then that's what you should do. 如果这意味着用方法暴露单个对象,那么这就是你应该做的。 This process almost inevitably leads to better code though in my experience. 根据我的经验,这个过程几乎不可避免地导致更好的代码。 As you've had to think more about what the module's purpose is, and this often leads to your breaking the coupling between pieces of code. 因为您必须更多地考虑模块的用途,这通常会导致您破坏代码片段之间的耦合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM