Example of URITemplate

by marbx

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.17/require.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap-theme.min.css">

JavaScript

// Config Object
requirejs.config({
    // Local Directory
    baseUrl: "/js",
    // Script Locations
    paths: {
        "URI": "//cdnjs.cloudflare.com/ajax/libs/URI.js/1.19.0/URI",
        "punycode": "//cdnjs.cloudflare.com/ajax/libs/URI.js/1.19.0/punycode",
        "IPv6": "//cdnjs.cloudflare.com/ajax/libs/URI.js/1.19.0/IPv6",
        "URITemplate": "//cdnjs.cloudflare.com/ajax/libs/URI.js/1.19.0/URITemplate",
        "SecondLevelDomains": "//cdnjs.cloudflare.com/ajax/libs/URI.js/1.19.0/SecondLevelDomains.min",

    }

});

// Check Dependencies
requirejs(['URITemplate'], function(URITemplate) {

    var template = new URITemplate("http://example.org/{file}");
    var result = template.expand({
        file: "hello world.html"
    });
    console.log(result);
    alert(result);
    try {
        var template = new URITemplate("http://example.org/{fi--le}");
        var result = template.expand({
            file: "hello world.html"
        });
    } catch (exception) {
        console.log(exception);
        alert(exception);
    }
    return {};

});