如何使用dojo以定义的顺序加载非AMD依赖项? [英] How can I load non-AMD dependencies in the defined order with dojo?

查看:320
本文介绍了如何使用dojo以定义的顺序加载非AMD依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用dojo加载非AMD库,我选择了一个单独的文件夹作为新的(伪)AMD包,并将该文件夹的文件放在该文件夹中,连同 main.js 文件,当请求包时,它将被dojo加载程序的约定加载。在这个main.js文件中,我简单地将我的库文件的名称放在 define()调用的依赖列表中。

  define([
jquery,
./lib/jquery-ui/jquery-ui.min,
。 /the.actual.library.that.uses.the.others,
],function(){
return window.someGlobalSetByTheLibrary;
});

这个问题是:依赖关系的加载顺序取决于网络延迟不能保证它们在 define()中显示的顺序。



导致(有时)错误,因为实际库文件需要自己的依赖关系才能加载。

解决方案

Dojo有一个完美的手段来帮助自己:加载插件



您可以编写自己的顺序加载插件,确保您的加载顺序。



(由于版权原因,我可以不要在这里发布实现,但是很容易使用dojo的 Deferred Promise 链接一个加载请求,内部使用 require()来进行实际加载)。



假设您的dojo加载程序插件具有模块id myPackage / sequentialLoading
然后,您的 main.js 将如下所示。

  define([
myPackage / sequentialLoading!jquery,
myPackage / sequentialLoading!./ lib / jquery-ui / jquery-ui.min,
myPackage / sequentialLoading! ./the.actual.library.that.uses.the.others,
],function(){
return window.someGlobalSetByTheLibrary;
});


I try to make a non-AMD library loadable with dojo, I've chosen a separate folder as a new (pseudo) AMD-package and placed the files of that library in that folder, together with the main.js file, which will get loaded by convention by the dojo loader when the package is requested. In this main.js file I simply put the names of my library files in the dependency list of the define() call.

define([
    "jquery", 
    "./lib/jquery-ui/jquery-ui.min",
    "./the.actual.library.that.uses.the.others",
], function () {
    return window.someGlobalSetByTheLibrary;
});

The problem with this is: the loading order of that dependencies depends on the network latency, it is not guaranteed to be the order they appear in the define().

That causes (sometimes) an error because the actual library file needs its own dependencies to be loaded first.

解决方案

Dojo has a perfect means to help yourself: loader plugins.

You can write your own sequential loading plugin that ensures the loading order for you.

(For copyright reasons of corporate owned code I can not post the implementation here, but it's quite easy using dojo's Deferred and Promise to chain one loading request after the other, internally using require() to do the actual loading).

Let's assume, your dojo loader plugin has the module id myPackage/sequentialLoading. Then, your main.js will look like this.

define([
    "myPackage/sequentialLoading!jquery",
    "myPackage/sequentialLoading!./lib/jquery-ui/jquery-ui.min",
    "myPackage/sequentialLoading!./the.actual.library.that.uses.the.others",
], function () {
    return window.someGlobalSetByTheLibrary;
});

这篇关于如何使用dojo以定义的顺序加载非AMD依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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