Requirejs' order does not work with priority config and CDN dependencies

不羁岁月 提交于 2019-12-30 05:29:16

问题


The following main.js code do not respect the order of priorities (sometimes underscore.js is not loaded when backbone.js needs it):

require({
    baseUrl:'/scripts',
    priority:[
        "http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js",
        "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js",
        "http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js",
        "http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"
    ]
    },["src/app"], 
    function (app) {
        app.start();
});

Adding order! before those CDN dependencies fails with a order.js not found error.


回答1:


I recently updated the RequireJS docs, but I have not pushed the change to the site yet:

The "priority" configuration cannot load resources loaded by plugins. So to accomplish what you are trying to do, you can just nest require() calls, which will give you the behavior you want:

require(
    {
        baseUrl:'/scripts'
    },
    [
        "require",
        "order!http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js",
        "order!http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js",
        "order!http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js",
        "order!http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"

    ], 
    function (require) {
        require(["src/app"], function (app) {
            app.start();
        });
    }
);

This assumes you have the order plugin in the /scripts/order.js location.



来源:https://stackoverflow.com/questions/6135259/requirejs-order-does-not-work-with-priority-config-and-cdn-dependencies

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