2
* RequireJS plugin for loading JSON files
3
* - depends on Text plugin and it was HEAVILY "inspired" by it as well.
4
* Author: Miller Medeiros
5
* Version: 0.3.1 (2013/02/04)
6
* Released under the MIT license
8
define(['text'], function(text){
10
var CACHE_BUST_QUERY_PARAM = 'bust',
11
CACHE_BUST_FLAG = '!bust',
12
jsonParse = (typeof JSON !== 'undefined' && typeof JSON.parse === 'function')? JSON.parse : function(val){
13
return eval('('+ val +')'); //quick and dirty
17
function cacheBust(url){
18
url = url.replace(CACHE_BUST_FLAG, '');
19
url += (url.indexOf('?') < 0)? '?' : '&';
20
return url + CACHE_BUST_QUERY_PARAM +'='+ Math.round(2147483647 * Math.random());
26
load : function(name, req, onLoad, config) {
27
if ( config.isBuild && (config.inlineJSON === false || name.indexOf(CACHE_BUST_QUERY_PARAM +'=') !== -1) ) {
28
//avoid inlining cache busted JSON or if inlineJSON:false
31
text.get(req.toUrl(name), function(data){
33
buildMap[name] = data;
36
onLoad(jsonParse(data));
40
accept: 'application/json'
46
normalize : function (name, normalize) {
47
//used normalize to avoid caching references to a "cache busted" request
48
return (name.indexOf(CACHE_BUST_FLAG) === -1)? name : cacheBust(name);
51
//write method based on RequireJS official text plugin by James Burke
52
//https://github.com/jrburke/requirejs/blob/master/text.js
53
write : function(pluginName, moduleName, write){
54
if(moduleName in buildMap){
55
var content = buildMap[moduleName];
56
write('define("'+ pluginName +'!'+ moduleName +'", function(){ return '+ content +';});\n');