如何在requireJS中回退到整个文件夹 [英] How to make a fallback to a whole folder in requireJS

查看:105
本文介绍了如何在requireJS中回退到整个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解了如何在requireJS中配置单个文件的后备,如下所示:

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'

        ]
    }
});

因此,如果没有"chrismas"皮肤,我将使用默认脚本.

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

我尝试用baseUrl做到这一点:

 var skin = "christmas";

        requirejs.config({

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

但是它失败了,并将baseUrl呈现为​​一个巨大的URL,并结合了2个字符串.

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

推荐答案

我误解了您的问题.我知道单个模块是可能的:

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

这篇关于如何在requireJS中回退到整个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆