RequireJS Download Line

My application has dynamic parts that are loaded from the database as a string that looks like this:

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

which is a simple requireJS module, like a string. I want lazy to load the script above using an asynchronous call. So my main requireJS script looks like this:

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

How to load this line in requireJS? I know about a text plugin, but it only allows files to be uploaded. I tried eval, but it does not correctly determine the dependencies.

+5
source share
3 answers

It's pretty late, but I'll just post my solution here if someone needs it.

, requireJS text! json! . String RequireJS - String, onLoad.fromText(), eval .

( db!):

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

:

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

:

  • require() eval. , onLoad.fromText() . eval , , , String eval(). , CSP.
  • , . , .
+5

, plugin :

define("load-string",[], function(){
    var strings=[],
        re_package_name = /^string_module_(\d+)$/;
    return {
        normalize: function(name, _){
            if(re_package_name.test(name)){
                return name
            }
            var nml = "string_module_" + (strings.push(name)-1);
            return nml;
        },
        load: function(name, _, onLoad, config){
            if(re_package_name.test(name)){
                onLoad.fromText(strings[name.match(re_package_name)[1]]);
            }else{
                onLoad.error("Invalid package name: ",name);
            }
        }  
    }
});

:

var world_module = "define([],function(){return 'world!'})";

require(["load-string!" + world_module],
    function(x){
        console.log("Hello "+x);
    })
0

You must be able to:

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

        var olddefine = define; // require js define 
        var runme; // capture function 
        define = function (args,func){
             runme = func; 
        }
        eval(scriptString);
        runme(); // run the function in current scope
        define = olddefine; // restore requirejs function 

        // dependency1, dependency2 are loaded        
    });
});
-1
source

All Articles