Created
September 30, 2011 21:25
-
-
Save millermedeiros/1255010 to your computer and use it in GitHub Desktop.
RequireJS plugin to load JSON files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* RequireJS plugin for loading JSON files | |
* - depends on Text plugin and it was HEAVILY "inspired" by it as well. | |
* | |
* IMPORTANT: it only works on r.js optimizer after version 0.26+ 20011/09/27 | |
* | |
* @author Miller Medeiros | |
* @version 0.0.1 (2011/06/10) | |
* Released under the WTFPL <http://sam.zoy.org/wtfpl/> | |
*/ | |
define(['text'], function(text){ | |
var jsonParse = (typeof JSON !== 'undefined' && typeof JSON.parse === 'function')? JSON.parse : function(val){ | |
return eval('('+ val +')'); //quick and dirty | |
}, | |
buildMap = {}; | |
//API | |
return { | |
load : function(name, req, onLoad, config) { | |
text.get(req.toUrl(name), function(data){ | |
if (config.isBuild) { | |
buildMap[name] = data; | |
onLoad(data); | |
} else { | |
onLoad(jsonParse(data)); | |
} | |
}); | |
}, | |
//write method based on RequireJS official text plugin by James Burke | |
//https://github.com/jrburke/requirejs/blob/master/text.js | |
write : function(pluginName, moduleName, write){ | |
if(moduleName in buildMap){ | |
var content = buildMap[moduleName]; | |
write('define("'+ pluginName +'!'+ moduleName +'", function(){ return '+ content +';});\n'); | |
} | |
} | |
}; | |
}); |
still using eval in https://github.com/millermedeiros/requirejs-plugins/blob/master/src/json.js
JSON.parse is widely supported.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cough
http://en.wikipedia.org/wiki/JSON#JavaScript_eval.28.29