Commits

Anonymous committed c99f120

Using example from data.gov.uk

Comments (0)

Files changed (2)

example/jquery/api.js

+// List all packages example
+
+$('#examples').append('<li id="list"><a href="#">List all packages</a></li>');
+$('#list').click(function(e) {
+    e.preventDefault();
+    $.ajax({
+        url: 'http://catalogue.data.gov.uk/api/rest/package',
+        type: 'GET',
+        success: function(data){
+            render_results(data);
+        },
+        error: function() {
+            alert('Failed to get the list of packages.')
+        },
+        dataType: 'jsonp',
+        jsonpCallback: 'jsonpcallback'
+    });
+});
+
+// Render a list of packages to the results <div>
+
+var render_results = function(packages) {
+    $('#result').html('<ul id="packages"></ul>');
+    for (var i=0; i<packages.length; i++) {
+        var elem = $('<li><a href="#">'+packages[i]+'</a></li>')
+        elem.click(function(e){
+            e.preventDefault();
+            var url = 'http://catalogue.data.gov.uk/api/2/rest/package/';
+            url += $(this).find('a').html();
+            $.ajax({
+                url: url,
+                type: 'GET',
+                success: function(data){
+                    var output = '<p>'
+                    for (name in data) {
+                        if (data.hasOwnProperty(name)) {
+                            if ( name == 'resources' ){
+                                var urls = '';
+                                for (var j=0; j<data[name].length; j++) {
+                                    urls += '<a href="'+data[name][j]['url']+'">';
+                                    urls += data[name][j]['url']+'</a>';
+                                }
+                                output += name+': '+urls+'<br />';
+                            } else {
+                                output += name+': '+data[name]+'<br />';
+                            }
+                        }
+                    }
+                    output += '</p>'
+                    $('#result').html(output);
+                },
+                error: function() {
+                    alert('Failed to search the packages.');
+                },
+                dataType: 'jsonp',
+                jsonpCallback: 'jsonpcallback'
+            });
+        });
+        $('#packages').append(elem);
+    };
+};
+
+
+// Search packages example
+
+$('#examples').append('<li id="search"><a href="#">Search package metadata</a></li>');
+$('#search').click(function(e) {
+    e.preventDefault();
+    var search_form = '';
+    search_form += '<form id="search_form">';
+    search_form += 'Search: <input type="test" id="terms" name="q" />';
+    search_form += '<input type="submit" value="Go" />';
+    search_form += '</form>';
+    $('#result').html(search_form);
+    $('#search_form').submit(function(e) {
+        e.preventDefault();
+        $.ajax({
+            url: 'http://catalogue.data.gov.uk/api/2/search/package',
+            type: 'GET',
+            data: {'q': $('#terms').val(), 'all_fields': '1'},
+            success: function(data){
+                alert('Found '+data['count']+' result(s)')
+                packages = []
+                for (var i=0; i<data['results'].length; i++){
+                    packages.push(data['results'][i]['name']);
+                }
+                render_results(packages);
+            },
+            error: function() {
+                alert('Failed to search the packages.');
+            },
+            dataType: 'jsonp',
+            jsonpCallback: 'jsonpcallback'
+        });
+    });
+})
+
+

example/jquery/index.html

+<!DOCTYPE html 
+    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
+    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">
+<head>
+<title>API Examples</title>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<script type="text/javascript" 
+    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
+</script>
+</head>
+<body>
+    <h1>API Examples</h1>
+    <ul id="examples">
+    </ul>
+    <div id="result"></div>
+    <script type="text/javascript" src="api.js"></script>
+</body>
+</html>