Null dependencies in RequireJS when ajax returns a 404

孤街醉人 提交于 2019-12-13 00:57:31

问题


Is there any way to have requireJS optionally (like maybe through a plugin) return null for a dependency that failed with a 404?

For example:

require(["allow404!myscript"], function(myscript){
    console.info(myscript);  // myscript should be null if myscript doesn't exist
});

回答1:


You could abuse paths config fallbacks to achieve this:

require(["myModuleOrNull"], function(myModuleOrNull) {
    console.log(myModuleOrNull);
});

in your requirejs config:

paths: {
    'myModuleOrNull': [
        'unreliable-module-location',
        // If above fails (timeout, 404, etc.) use the one below
        'null-module'
    ]
}

and the null-module.js:

define([], function() {
    return null;
});

...but why would you want to do that? Handling optional null where a module is expected will be nothing but pain. Is there some specific reason for doing this?



来源:https://stackoverflow.com/questions/15751642/null-dependencies-in-requirejs-when-ajax-returns-a-404

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