简体   繁体   English

带有require.JS的主干如何工作?

[英]How does backbone with require.JS work?

I am trying to understand how requireJS and backbone work. 我试图了解requireJS和骨干网是如何工作的。 With this site when I open the DevTools in Chrome and opened a Sources Tab. 这个网站上,当我在Chrome中打开DevTools并打开“源”选项卡时。 I see a long list of folders/files (see image below) which seems to be the source code when it's uncompressed. 我看到一长串的文件夹/文件(请参见下图),当未压缩时,这似乎是源代码。 However, I don't see it being loaded via the Network Tab. 但是,我看不到它是通过“网络”选项卡加载的。

资料夹清单

I wonder how does it tie in and is it normal that the source code gets exposed like this, and whether it is normal that all views are being loaded even I just requested one page (ie the search page, see image below). 我想知道它是如何连接的,这样公开源代码是正常的,即使我只是请求一个页面(例如,搜索页面,请参见下图),所有视图都被加载是否正常? I understand that modern Javascript applications like Angular likes to preload the application before it's presented. 我了解到,像Angular这样的现代Javascript应用程序喜欢在应用程序出现之前对其进行预加载。 But wouldn't it be causing a lot of unnecessary traffic to users? 但这会不会给用户带来很多不必要的流量? especially those on mobile view? 尤其是那些在移动设备上观看的用户

较长的视图列表不在搜索视图中

First, your question mixes two things. 首先,您的问题有两点。

  • BackboneJS and RequireJS are completely unrelated BackboneJS和RequireJS完全无关
  • What you observe (module dependency structure in the scripting panel vs. all those files actually not downloaded) is due to the Debugger support of "Source maps" 您观察到的内容(脚本面板中的模块依赖性结构与实际上未下载的所有那些文件)是由于“源映射”的调试器支持

Since I guess your confusion is caused by this I'll start with ... 由于我想您的困惑是由此引起的,因此我将从...开始

Source maps 源图

Modern browsers support source maps. 现代浏览器支持源地图。 Their intention is to reveal the original source code when it has been concatenated and/or minified into one file. 他们的目的是在将原始源代码连接和/或缩小为一个文件后,将其显示出来。

The source map file describes eg the symbol ZZyb at line 1 char 20563 is mapped to eg the file/somewhere/in/the/tree at line 34 char 1 and is named someView . 源映射文件描述了例如第1行char ZZyb处的符号ZZyb映射到第34行char 1处例如file/somewhere/in/the/tree并被命名为someView

Minified files reference the source map using the 缩小的文件使用

# sourceMappingURL=getaround-min.js.map

signature. 签名。

More on source maps: http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/ 有关源地图的更多信息: http : //www.html5rocks.com/en/tutorials/developertools/sourcemaps/

When downloading the minified file ( https://www.getaround.com/js/150502002818/getaround-min.js ) on that website you've linked, you will observe that signature at the end of the file: 在您链接的网站上下载缩小文件( https://www.getaround.com/js/150502002818/getaround-min.js )时,您会在文件末尾看到该签名:

//# sourceMappingURL=getaround-min.js.map //#sourceMappingURL = getaround-min.js.map

You can then download that file. 然后,您可以下载该文件。 This is what your debugger does. 这就是调试器的工作。

RequireJS vs. BackboneJS RequireJS与BackboneJS

You can use RequireJS to modularize your own code or in conjunction with other Frameworks that do not already ship with such technologies. 您可以使用RequireJS来模块化您自己的代码,或者与尚未随此类技术一起提供的其他框架结合使用。

AngularJS for example has it's own dependency management which allows you to define named dependencies and finally you start the application. 例如,AngularJS拥有自己的依赖项管理,可让您定义命名的依赖项,最后启动应用程序。 This allows you to just concatenate (and minify) all sources into one file without taking care of the definition order. 这样,您就可以将所有源代码串联(并缩小)到一个文件中,而无需考虑定义顺序。

Even though Backbone and Require are unrelated they work very well together. 即使Backbone和Require无关,它们也可以很好地协同工作。

RequireJS RequireJS

RequireJS implements the so called AMD spec . RequireJS实现了所谓的AMD规范

A module defines dependencies and a callback to implement that module. 模块定义依赖关系和回调以实现该模块。

depA: DEPA:

// Require depB and depC and after they've been loaded 
// call the callback function and pass those 
// dependencies. Finally return that module.
define(["depB", "depC"], function(depB, depC){
   // by convention depB will resolve to depB.js relative 
   // to "depA"'s path

   // object, string, number, function your module is made of
   return something; 
});

RequireJS will download the dependencies, store (cache) them intenally and pass them to the callback. RequireJS将下载依赖项,并定期存储(缓存)并将其传递给回调。 This process repeats down the dependency tree. 此过程重复依赖树。 When a dependency has already been downloaded (by another upstream module) it can be passed directly without downloading it again. 当依赖项已经被下载(通过另一个上游模块)时,可以直接传递它,而无需再次下载。

depB: DEPB:

define(["depC", "depD"], function(depC, depD){
   // depC has already been loaded and cached - doesn't 
   // need to be downloaded again
});

RequireJS optimization RequireJS优化

Downloading all dependencies file by file can (and - on internet seites - should) be avoided by packaging them using r.js . 通过使用r.js打包可以避免逐个文件下载所有依赖项(以及在互联网上)。 The modules will be converted into into one file during build time. 在构建期间,模块将转换为一个文件。

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

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