How to make a fallback to a whole folder in requireJS

我是研究僧i 提交于 2019-12-13 08:26:00

问题


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:


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 () {});
         }
      }
});


来源:https://stackoverflow.com/questions/13973105/how-to-make-a-fallback-to-a-whole-folder-in-requirejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!