require.js 同步加载 [英] require.js synchronous loading

查看:63
本文介绍了require.js 同步加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义一个模块来计算新的依赖关系,获取它然后返回结果.像这样:

I'd like to define a module which computes a new dependancy, fetches it and then returns the result. Like so:

define(['defaults', 'get_config_name'], function(defaults, get_config_name) {
    var name = get_config_name();
    var config;
    require.synchronous([configs / '+name'], function(a) {
        config = defaults.extend(a);
    });
    return config;
});

有没有办法做到这一点或有更好的方法来解决这个问题?

Is there a way to do this or a better way to attack this problem?

推荐答案

  • 你可以尝试使用同步RequireJS调用 require('configs/'+get_config_name()),但是只有已经加载才会同步加载模块,否则会抛出异常.同步加载模块/JavaScript 文件在技术上是不可能的.UPD: 这是可能的(参见 Henrique 的回答),但非常不推荐.它会阻止导致整个页面冻结的 JavaScript 执行.所以,RequireJS 不支持它.

    • You may try to use synchronous RequireJS call require('configs/'+get_config_name()), but it will load a module synchronously only if it is already loaded, otherwise it will throw an exception. Loading module/JavaScript file synchronously is technically impossible. UPD: It's possible (see Henrique's answer) but highly unrecommended. It blocks JavaScript execution that causes to freezing of the entire page. So, RequireJS doesn't support it.

      从您的用例来看,您似乎不需要同步 RequireJS,您需要异步返回结果.AMD 模式允许定义依赖项并异步加载它们,但模块的工厂函数必须同步返回结果.解决方案可能是使用 loader 插件(详情此处这里):

      From your use case it seems that you don't need synchronous RequireJS, you need to return result asynchronously. AMD pattern allows to define dependencies and load them asynchronously, but module's factory function must return result synchronously. The solution may be in using loader plugin (details here and here):

      // config_loader.js
      define(['defaults', 'get_config_name'], function(defaults, get_config_name) {
          return {
              load: function (resourceId, require, load) {
                  var config_name = 'configs/' + get_config_name();
                  require([config_name], function(config) {
                      load(defaults.extend(config));
                  })
              }
          }
      });
      
      // application.js
      define(['config_loader!'], function(config) {
          // code using config
      });
      

    • 如果 get_config_name() 包含简单的逻辑并且不依赖于其他模块,则动态计算更好更简单路径配置选项,或者如果您的配置取决于上下文 - 地图配置选项.

    • If get_config_name() contains simple logic and doesn't depend on another modules, the better and simpler is calculating on the fly paths configuration option, or in case your config depends on context - map configuration option.

      function get_config_name() {
          // do something
      }
      require.config({
          paths: {
              'config': 'configs/' + get_config_name()
          }
      });
      require(['application', 'defaults', 'config'], function(application, defaults, config) {
          config = defaults.extend(config);
          application.start(config);
      });
      

    • 这篇关于require.js 同步加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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