Search code examples
javascriptangularjscdnfallback

How can I use fallback js with angular modules?


I'm using fallback to load a local file if the cdn is unavailable;

The readme states: "The key must be the libraries window variable" - for example jQuery (or $ I guess) for jquery.

fallback.load({
    // [...]
    jQuery: [
        '//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.FAIL_ON_PURPOSE.min.js',
        '//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js',
        '//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.0/jquery.min.js'
    ],

    'jQuery.ui': [
        '//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js',
        '//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js',
        '//js/loader.js?i=vendor/jquery-ui.min.js'
    ]
}, //[...] shim, callback etc 
});

However, when loading angularjs modules or jquery plugin, I do not have a window library to use as a test.

Is there any way to use the library?


Solution

  • Yes, there is: - just use an expression that would return undefined if the library is not loaded:

    This is the github issue and this is the plunker:

    fallback.load({
        //[...]
        'angular.module("ngRoute")': [
                '//ajax.googleapis.com/ajax/libs/angularjs/1.2.3/angular-route.min.js'
            ],
        //[...]
    });