任意排序的Javascript依赖解决方案包括 [英] Javascript dependency solution with arbitrary ordered includes

查看:94
本文介绍了任意排序的Javascript依赖解决方案包括的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是这个,我包括一个脚本,它使用另一个脚本,但是如果依赖还需要另一个脚本来准备好呢?这意味着加载它是不够的,但是我也需要确保在执行之前调用它的初始化回调。如果依赖关系被允许需要更多的依赖关系并在加载后将其显示出来,则DOM中的脚本标记的顺序是不正确的。



问题变得更加复杂脚本和依赖项需要多个依赖关系,或者文件满足多个组件。



Using.js似乎有一个很好的方法,但首先,依赖关系应该在加载之前被知道,第二个作者说,一些测试是在Firefox上不再工作了我怀疑这是阻止执行的东西,这似乎有点神奇。



我已经编写了一个加载程序来处理这个,完全异步,这实际上似乎是工作。但是我不能动摇做某事的直觉,以前已经解决了,或者不能那么复杂。

解决方案


如果依赖关系被允许需要更多依赖并在加载后显示,则DOM中的脚本标记的顺序是不正确的。


那么这些脚本是试图加载自己的依赖关系呢?这不是JavaScript的标准功能,所以没有一个答案;对于同步加载的脚本(通过document.write加载脚本元素或者在加载的DOM中添加脚本元素)时,你如何处理它是特定于试图包含依赖性的脚本。



< - 时间),你至少可以肯定你很好去上班。对于异步加载(延迟脚本,AJAX包含或由超时触发的即时回调),必须有一个回调机制。如果要混合具有不同依赖关系和即时回调系统的各种脚本,这将是一个痛苦。


我已经写了一个加载器处理这个,完全异步的,其实似乎是工作。但是我不能动摇做某事的直觉,以前已经解决了,或者不能那么复杂。


不,不是真的。依赖关系处理和动态脚本插入总是有点复杂。唯一的已解决的标准方法是完全静态的脚本,它们以依赖顺序手动编写。


The problem is this, I include a script, it uses another one, but what if that dependency also needs another script to be ready ? Which means loading it is not enough, but I also need to be sure its callback for initialization has been called before executing. The order of script tags in DOM can not be correct if the dependencies are allowed to require more dependencies and manifest them when them after being loaded.

The problems gets more complex when scripts and dependencies require more than one dependency, or a file satisfies more than one component.

Using.js seems to have a good approach for this, but first, the dependency relations should be known before they are being loaded, second the author states that some tests are not working anymore on Firefox. I suspect it is the blocking the execution thing, which seems a bit magical.

I have written a loader to handle this, completely asynchronous, which actually seems to be work. But I can not shake the gut feeling of doing something has been solved before, or can not be that complex.

解决方案

The order of script tags in DOM can not be correct if the dependencies are allowed to require more dependencies and manifest them after being loaded.

Well, what are these scripts that try to load their own dependencies, exactly? This is not a standard feature of JavaScript, so there is not one answer; how you cope with it is specific to the script that tries to include dependencies.

For synchronously loaded scripts (included via document.write or adding script elements to the DOM at load-time) you can at least be sure that you're good to go onload. For asynchronous loading (deferred scripts, AJAX inclusion, or ready-callbacks fired by timeout) there has to be a callback mechanism. If you are mixing various scripts that have different dependency and ready-callback systems, that's going to be a pain.

I have written a loader to handle this, completely asynchronous, which actually seems to be work. But I can not shake the gut feeling of doing something has been solved before, or can not be that complex.

No, not really. Dependency handling and dynamic script insertion are always going to be a little bit complicated. The only ‘solved’, standard approach is completely static scripts, writing them in dependency order manually.

这篇关于任意排序的Javascript依赖解决方案包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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