2013-07-18 56 views
2

我已经使用RequireJS加载jQuery和其他插件,具体如下:获取使用RequireJS加载的文本文件的内容?

var dependencies = ["order!jquery", "order!plugins/jquery.typewatch", 
    "order!plugins/jquery.csv-0.71.min", "plugins/jquery.zclip.min"]; 
require(dependencies, function($) { 
    // jQuery code goes here 
}); 

这工作得很好。我现在想用RequireJS加载一个文本文件,所以我已经下载了RequireJS text plugin,并修改了我的代码:

var dependencies = ["order!jquery", "order!plugins/jquery.typewatch", 
    "order!plugins/jquery.csv-0.71.min", "plugins/jquery.zclip.min", 
    "text!data/mytext.txt"]; 
require(dependencies, function($) { 
    // jQuery code goes here 
}); 

望着DevTools,请求工作正常 - 所请求的文本文件。但是如何获得文本文件的内容?

我想这一点,作为建议由RequireJS文档:

require(dependencies, function($, txt) { 
    console.log(txt); 
}); 

txt是不确定的。

UPDATE:如果是相关的,我使用RequireJS 1.0.7,并调用它从我的HTML如下:

<script data-main="scripts/main.js" src="/scripts/require-jquery.js"></script> 

回答

1

每个函数参数对应于装载依赖的结果,在指定依赖关系列表。您需要在函数声明为很多争论的依赖,或将text!data...依赖于前:

var dependencies = ["order!jquery", "text!data/mytext.txt", ....]; 
require(dependencies, function($, txt) { 
    console.log(txt); 
}); 
+0

如此简单 - 我真的应该尝试的。谢谢! – Richard