如何从dojo插件中的require failure中恢复?

我有一个要求,我需要将模块列表传递给插件,并让它加载模块并执行一些工作.如果我传递了一个无法加载的模块,我应该报告错误并转到列表的其余部分.我被困了,因为我无法弄清楚如何从坏模块的要求失败中恢复.我可以用其他技术来满足这个要求吗?这是一个在没有我所有其他要求的情况下将问题提炼下来的示例,我需要从加载my / thing2失败中恢复:

define("my/thing",[],function() {
    return 'thing';
});
define("my/loader",function() {
    return {
        load: function(mid,require,callback) {
            console.log('inside load',arguments);

            // is there some way to recover when this require fails
            // or some other technique I can use here?
            try {
                require([mid],function(mod) {
                    console.log('inside require,mod=',mod);
                    callback(mod);
                });
            }
            catch (error) {
                // never gets here,when the require fails everything just stops
                console.log(error);
                callback("failed to load " + mid);
            }
        }
    }
});

require(["my/loader!my/thing"],function(loaded) {
    console.log('loaded',loaded);
});

require(["my/loader!my/thing2"],loaded);
});

解决方法

如果您严格要求忽略无效或有故障的模块并继续下一个模块,请在将它们放入require语句之前使用 dojo/_base/lang::exists()

require(['/dojo/_base/lang','dojo/text!my/thing2'],function(lang,myThing2) {
    if(lang.exists(myThing2)) {
        //success
    } else {
        //failure
    }
});

相关文章

我有一个网格,可以根据更大的树结构编辑小块数据.为了更容易...
我即将开始开发一款教育性的视频游戏.我已经决定以一种我可以...
我正在使用带有Grails2.3.9的Dojo1.9.DojoNumberTextBox小部...
1.引言鉴于个人需求的转变,本系列将记录自学arcgisapiforja...
我正在阅读使用dojo’sdeclare进行类创建的语法.描述令人困惑...
我的团队由更多的java人员和JavaScript经验丰富组成.我知道这...