Question

I am already using RequireJS to load jQuery and other plugins, as follows:

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

This works fine. I now want to use RequireJS to load a text file, so I've downloaded the RequireJS text plugin, and amended my code:

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

Looking at DevTools, the request is working OK - the text file is being requested. But how do I get hold of the content of the text file?

I tried this, as suggested by the RequireJS docs:

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

but txt is undefined.

UPDATE: if it's relevant, I'm using RequireJS 1.0.7, and invoking it as follows from my HTML:

<script data-main="scripts/main.js" src="/scripts/require-jquery.js"></script>
Was it helpful?

Solution

Each function argument corresponds to the result of loading a dependency, specified in the list of dependencies. You need to either declare as many arguments as dependencies in the function, or move the text!data... dependency to the front:

var dependencies = ["order!jquery", "text!data/mytext.txt", ....];
require(dependencies, function($, txt) {
    console.log(txt);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top