微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从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
    }
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐