json.html 1.7 KB
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>RequireJS JSON plugin</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <div id="wrapper">
            <h1>RequireJS JSON plugin</h1>
            <p>Helper for loading JSON files, it will also work during optimization (wrapping JSON files into a `define` call).</p>
            <p>If you want to load JSONP data use the `async` plugin instead.</p>
            <p>You can set the flag <code>`!bust`</code> to prevent caching the JSON response, it will append a query argument <code>&quot;bust=RANDOM_INTEGER&quot;</code> to the URI.</p>
            <h2>Output:</h2>
            <div id="output" style="border:1px solid #ccc; background:#f5f5f5; padding:10px 20px"></div>
        </div>
        <script src="../lib/require.js"></script>
        <script>
            require.config({
                waitSeconds : 2,
                paths : {
                    text : '../lib/text', //text is required
                    json : '../src/json' //alias to plugin
                }
            });

            // adding the flag `!bust` to the end of dependency name will avoid caching
            require(['json!data/foo.json', 'json!data/bar.json!bust'], function(foo, bar){
                var out = document.getElementById('output');
                //data is parsed into an object
                out.innerHTML += '<p><b>lorem:<\/b> '+ foo.lorem +'<\/p>';
                out.innerHTML += '<p><b>bar:<\/b> '+ foo.bar +'<\/p>';
                out.innerHTML += '<p><b>message:<\/b> '+ bar.text +'<\/p>';
            });
        </script>
    </body>
</html>