我们需要在我们的网络应用程序中实施并正在努力。现在的变化是我们有一些在客户端运行时动态创建的javascript。有没有办法让require直接将一串javascript直接注入require()函数。
目前我们有:
require([moduleToLoad], function(mod){ //DO SOMETHING}});
想要类似的东西(原谅语法因为只写出来):
var jsString = "define(['Text!something.htm'], function (control_Template) { //MODULE LOADED JAVASCRIPT}))";
require(jsString, function(mod){ //DO SOMETHING}});
有谁知道我们是如何做到这一点的?
由于
答案 0 :(得分:0)
您可以在字符串中使用命名模块语法来定义AMD模块。 Eval此字符串,然后使用您提供的名称要求模块。
定义模块如下
eval("define('runtimemodule', ['Text!something.htm'], function(){ return 'template here' })")
需要模块如下
require(["runtimemodule"], function(mod){
console.log(mod); //logs "tempalte here"
});