How to make a fallback to a whole folder in requir

2019-08-26 09:09发布

I understood how I can config in requireJS a fallback for a single file like this:

    var skin = "christmas";

    requirejs.config({

    paths: {
        languagesLocal : [
            BaseUrl +  skin + '/js/languagesLocal',
            BaseUrl +  "default" + '/js/languagesLocal'

        ]
    }
});

so incase there is no "chrismas" skin I land to the default script.

Now - my question is - How do I make a complete folder to be the fallback folder? so incase one file does not exist in the skin folder - it will have a fallback in another folder. I need that each file will be checked if it exist and if not it should have a fallback in the default folder

I tried to do it with baseUrl:

 var skin = "christmas";

        requirejs.config({

       baseUrl: [
        BaseUrl +  skin + '/js/',
        BaseUrl +  'default' + '/js/'
        ],
    });

but it fails and render the baseUrl as one huge URL combined with the 2 strings.

1条回答
Animai°情兽
2楼-- · 2019-08-26 09:47

Edit: I misinterpreted your question. I know it is possible for single modules:

http://requirejs.org/docs/api.html#errbacks

With this technique, you could create a module which would verify each module's existance:

loader.js

define(['a', 'b'], function(a, b) {}, 
   function(err) {
      if (err.requireModules) {
         for(var i = 0; i < err.requireModules.length; i++) {
            requirejs.undef(requireModules[i]);

            requirejs.config({
               baseUrl: BaseUrl +  'default' + '/js/'
            });
            require([requireModules[i]], function () {});
         }
      }
});
查看更多
登录 后发表回答