How can I load non-AMD dependencies in the defined order with dojo?

情到浓时终转凉″ 提交于 2019-12-10 10:39:18

问题


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.


回答1:


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;
});


来源:https://stackoverflow.com/questions/26531409/how-can-i-load-non-amd-dependencies-in-the-defined-order-with-dojo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!