aboutsummaryrefslogtreecommitdiffstats
path: root/syte/static/js/libs/json.js
blob: fbf3fbc029e5e905f1ba36c44675863e13987e62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*!
 * 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');
            }
        }

    };
});