RequireJS加载字符串 [英] RequireJS load string

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

问题描述

在我的应用程序中,有一些动态部分从数据库加载为字符串,如下所示:

In my app there are dynamic parts that are loaded from database as string that looks like:

"define(['dependency1', 'dependency2'], function(){"+
"   // function body" +
"})"

这只是一个简单的requireJS模块,作为字符串。我想使用async require调用延迟加载上面的脚本。所以,我的main requireJS脚本如下所示:

which is just a simple requireJS module, as a string. I want to lazy load the script above using async require call. So, my main requireJS script looks like:

require(["jquery"], function($){        
    $(document).ready(function(){
        // logic to load specific script from database
        var scriptString = functionToLoadTheStringAbove();

        // ideally i would like to call it like this            
        require([scriptString], function(){
           // scriptString, dependency1, dependency2 are loaded
        }
    });
});

如何在requireJS中加载这些字符串?我知道文本插件,但它只允许从文件。我试过eval但它没有正确解决依赖关系。

How do I load those string in requireJS? I know about text plugin, but it only allow loading from files. I tried eval but it doesn't resolve dependencies correctly.

推荐答案

这已经很晚了,但我只是发布我的解决方案这里以防任何人需要。

This is quite late, but I just post my solution here in case anyone needs.

所以我最后在requireJS论坛上询问并检查 text!plugin json!插件。在RequireJS中从String加载模块的最简单方法是使用自己的插件加载String,然后使用 onLoad.fromText( ) eval 您的String并解析所有依赖项。

So I ended up asking in requireJS forum and examining the source of text! plugin and json! plugin. The cleanest way to load module from String in RequireJS is by making your own plugin to load the String, and then use onLoad.fromText() that will eval your String and resolve all dependencies.

我的插件示例(让我们称之为 db!插件):

Example of my plugin (let's call it db! plugin):

define([], function(){
    var db = new Database(); // string is loaded from LocalStorage
    return {
        load: function(name, req, onLoad, reqConfig){
            db.get(name, function(err, scriptString){
                if (err) onLoad(err);
                else onLoad.fromText(scriptString);
            });  
         }
    }
});

然后您可以使用以下插件:

You can then use the plugin like:

require(["jquery", "db!myScript"], function($, myScript){        
    // jQuery, myScript and its dependencies are loaded from database
});

注意:


  1. 没有 require <)来自没有 eval 的字符串。这是 onLoad.fromText()在内部执行的操作。因为eval是邪恶的,所以如果你知道你将要使用什么字符串 eval(),你应该只使用它。如果您在浏览器扩展中使用它,则可能需要放宽CSP策略。

  2. 要命名您的String模块,您可以使用显式命名语法。这样,您的模块将始终具有相同的绝对名称。

  1. There's no way to require() from String without eval. This is what onLoad.fromText() does internally. Since eval is evil, you should only use it if you know what String you're going to eval(). If you're using it in browser extension, you might want to relax the CSP policy.
  2. To name your String module, you can use explicit naming syntax. This way, your module will always have the same absolute name.

这篇关于RequireJS加载字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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