简体   繁体   English

延迟加载和依赖项解析

[英]Lazy loading and dependency resolution

some time ago, I was reading an article(a library built by some guy) about how his library can do 前段时间,我在读一篇文章(某人建立的图书馆),介绍他的图书馆的工作方式

  1. lazy loading of JS JS的延迟加载
  2. resolve dependencies between JS (typically encountered when trying to "include" one js from another) 解决JS之间的依赖关系(通常在尝试从另一个“包含”一个js时遇到)
  3. include files only once. 仅包含一次文件。 thought specified multiple times regardless of how they are called (either directly specifying it as file or specifying it as one of the dependencies) 思想多次指定,无论如何调用(直接将其指定为文件或将其指定为依赖项之一)

I forgot to bookmark it, what a mistake . 我忘了给它加上书签,真是个错误 Can some one point me to something which can do the above. 可以有人指出我可以做上述事情的事情吗? I know DOJO and YUI library have something like this, but I am looking for something which I can use with jQuery 我知道DOJO和YUI库有这样的东西,但是我正在寻找可以与jQuery一起使用的东西

I am probably looking for one more feature as well. 我可能也在寻找其他功能。

  • My site has asp.net user controls (reusable server side code snippets) which have some JS. 我的站点具有一些JS的asp.net用户控件(可重用的服务器端代码段)。 Some of them get fired right away, when the page is loading which gives a bad user experience. 当页面加载时,其中一些立即被解雇,这给用户带来了糟糕的体验。 Yahoo performance guidelines specify that JS should be at the bottom of the page, but this is not possible in my case as this would require me to separate the JS and the corresponding server side control into different files and maintenance would be difficult. Yahoo性能准则指定JS应该位于页面的底部,但是在我的情况下这是不可能的,因为这将需要我将JS和相应的服务器端控件分离到不同的文件中,并且很难维护。 I definitely can put a jQuery document.ready() in my user control JS to make sure that it fires only after the DOM has loaded, but I am looking for a simpler solution. 我绝对可以在用户控件JS中放入jQuery document.ready()以确保仅在DOM加载后才触发,但是我正在寻找一种更简单的解决方案。

Is there anyway that I could say "begin executing any JS only after DOM has loaded" in a global way than just writing "document.ready" within every user control ? 无论如何,我可以说以一种全局的方式说“仅在DOM加载后才开始执行任何 JS”,而不是在每个用户控件中编写“ document.ready”吗?

Microsoft Research proposed a new tool called DOLOTO . 微软研究院提出了一种名为DOLOTO的新工具。 It can take care of rewriting & function splitting and enable the on-demand js loading possible. 它可以处理重写和功能拆分,并启用按需js加载。

From the site.. 来自网站。

Doloto is a system that analyzes application workloads and automatically performs code splitting of existing large Web 2.0 applications. Doloto是一个系统,可以分析应用程序工作负载并自动执行现有大型Web 2.0应用程序的代码拆分。 After being processed by Doloto, an application will initially transfer only the portion of code necessary for application initialization. 在由Doloto处理之后,应用程序最初将仅传输应用程序初始化所需的部分代码。 The rest of the application's code is replaced by short stubs -- their actual function code is transferred lazily in the background or, at the latest, on-demand on first execution. 应用程序的其余代码被短存根(stub)取代-它们的实际功能代码在后台延迟传输,或者最迟在首次执行时按需传输。

OK I guess I found the link 好吧,我想我找到了链接

http://ajaxian.com/archives/usingjs-manage-javascript-dependencies http://www.jondavis.net/techblog/post/2008/04/Javascript-Introducing-Using-%28js%29.aspx http://ajaxian.com/archives/usingjs-manage-javascript-dependencies http://www.jondavis.net/techblog/post/2008/04/Javascript-Introducing-Using-%28js%29.aspx

I also found one more, for folks who are interested in lazy loading/dynamic js dependency resolution 我还为那些对延迟加载/动态js依赖项解析感兴趣的人找到了一个

http://jsload.net/ http://jsload.net/

About the lazy-loading scripts thingy, most libraries just adds a <script> element inside the HTML pointing to the JS file to be "included" (assynchronously), while others like DOJO, fetches it's dependencies using a XMLHttpRequest and then eval's its contents, making it work synchronously. 关于延迟加载脚本的问题,大多数库只是在HTML内添加一个<script>元素,指向要“包含”的JS文件(异步地),而其他的对象(如DOJO)则使用XMLHttpRequest来获取其依赖项,然后评估其内容,使其同步工作。

I've used the YUI-Loader that is pretty much simple to use and you don't need the whole library to get it working. 我使用了非常简单易用的YUI-Loader,并且不需要整个库就可以使它工作。 There are other libraries that gives you just this specific funcionality, but I think YUI's is the safe choice. 还有其他库可以为您提供这种特定的功能,但是我认为YUI是安全的选择。

About your last question, I don't think there's something like that. 关于您的最后一个问题,我认为没有这样的事情。 You would have to do it yourself, but it would be similar to using document.ready . 您必须自己做,但这类似于使用document.ready

i did in my framework a similar thing: 我在我的框架中做了类似的事情:
i created a include_js(file); 我创建了一个include_js(file); that include the js file only if it isn't included reading and executing it with a synchronous ajax call. 仅在不包含js文件的情况下,才通过同步ajax调用读取和执行该文件。 Simply put that code in top of the page that needs dependencies and you're done! 只需将代码放在需要依赖项的页面顶部,就可以完成!

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

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