~caneypuggies/reformedchurcheslocator/couchapp-backbone

« back to all changes in this revision

Viewing changes to _attachments/js/vendor/requirejs-plugins/src/json.js

  • Committer: Tim Black
  • Date: 2013-09-16 22:50:16 UTC
  • Revision ID: tim@alwaysreformed.com-20130916225016-zk8jiba25z33ew7h
Versioned Bower vendor directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @license
 
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
 
7
 */
 
8
define(['text'], function(text){
 
9
 
 
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
 
14
        },
 
15
        buildMap = {};
 
16
 
 
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());
 
21
    }
 
22
 
 
23
    //API
 
24
    return {
 
25
 
 
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
 
29
                onLoad(null);
 
30
            } else {
 
31
                text.get(req.toUrl(name), function(data){
 
32
                    if (config.isBuild) {
 
33
                        buildMap[name] = data;
 
34
                        onLoad(data);
 
35
                    } else {
 
36
                        onLoad(jsonParse(data));
 
37
                    }
 
38
                },
 
39
                    onLoad.error, {
 
40
                        accept: 'application/json'
 
41
                    }
 
42
                );
 
43
            }
 
44
        },
 
45
 
 
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);
 
49
        },
 
50
 
 
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');
 
57
            }
 
58
        }
 
59
 
 
60
    };
 
61
});