javascript - Handling prerequsites load failure in RequireJS require function - Stack Overflow

I'm using RequireJS for AMD. Using this code I execute my function after ensuring the module1 is l

I'm using RequireJS for AMD. Using this code I execute my function after ensuring the module1 is loaded:

require(['module1'], function (module1) {
    if (module1) {
        // My function code...
    }
); 

In some cases the module1 is not available (mostly because of access security). I want to handle what happens if module1 failed to load. Using some code like:

require(['module1'], function (module1) {
    if (module1) {
        // My function code...
    }
)
.fail(function(message)
{
    console.log('error while loading module: ' + message);
}

or maybe the require function accepts another parameter for module load failures?

So the question is, how can I handle if the required module failed to load?

I'm using RequireJS for AMD. Using this code I execute my function after ensuring the module1 is loaded:

require(['module1'], function (module1) {
    if (module1) {
        // My function code...
    }
); 

In some cases the module1 is not available (mostly because of access security). I want to handle what happens if module1 failed to load. Using some code like:

require(['module1'], function (module1) {
    if (module1) {
        // My function code...
    }
)
.fail(function(message)
{
    console.log('error while loading module: ' + message);
}

or maybe the require function accepts another parameter for module load failures?

So the question is, how can I handle if the required module failed to load?

Share Improve this question edited Apr 25, 2014 at 9:57 jgillich 76.6k7 gold badges60 silver badges88 bronze badges asked Oct 13, 2013 at 8:22 mehrandvdmehrandvd 9,14614 gold badges67 silver badges119 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

See RequireJS API document: http://requirejs/docs/api.html#errors.

require(['jquery'], function ($) {
    //Do something with $ here
}, function (err) {
    //The errback, error callback
    //The error has a list of modules that failed
    var failedId = err.requireModules && err.requireModules[0];
    if (failedId === 'jquery') {
        //undef is function only on the global requirejs object.
        //Use it to clear internal knowledge of jQuery. Any modules
        //that were dependent on jQuery and in the middle of loading
        //will not be loaded yet, they will wait until a valid jQuery
        //does load.
        requirejs.undef(failedId);

        //Set the path to jQuery to local path
        requirejs.config({
            paths: {
                jquery: 'local/jquery'
            }
        });

        //Try again. Note that the above require callback
        //with the "Do something with $ here" ment will
        //be called if this new attempt to load jQuery succeeds.
        require(['jquery'], function () {});
    } else {
        //Some other error. Maybe show message to the user.
    }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745157009a4614184.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信