|
| 1 | +// Example of Standard Connections in Web Data Connectors using JSONPlaceholder JSON endpoints |
| 2 | +// Tableau 10.1 - WDC API v2.1 |
| 3 | + |
| 4 | +// Define our Web Data Connector |
| 5 | +(function(){ |
| 6 | + var myConnector = tableau.makeConnector(); |
| 7 | + myConnector.getSchema = function(schemaCallback) { |
| 8 | + // Create a promise to get our Standard Connections List from a JSON file. This increases code readability since we |
| 9 | + // no longer need to define the lengthy object within our javascript itself. |
| 10 | + var standardConnections = new Promise(function(resolve, reject) { |
| 11 | + loadJSON("StandardConnectionsData", function(json) { |
| 12 | + var obj = JSON.parse(json); |
| 13 | + var connectionList = []; |
| 14 | + for (var connection in obj.connections) { |
| 15 | + connectionList.push(obj.connections[connection]); |
| 16 | + } |
| 17 | + resolve(connectionList); |
| 18 | + }, true); |
| 19 | + }); |
| 20 | + // Create a promise to get our table schema info as well, just like above |
| 21 | + var tables = new Promise(function(resolve, reject) { |
| 22 | + loadJSON("StandardConnectionsTableInfoData", function(json) { |
| 23 | + var obj = JSON.parse(json); |
| 24 | + var tableList = []; |
| 25 | + for (var table in obj.tables) { |
| 26 | + tableList.push(obj.tables[table]); |
| 27 | + } |
| 28 | + resolve(tableList); |
| 29 | + }, true); |
| 30 | + }); |
| 31 | + // Once all our promises are resolved, we can call the schemaCallback to send this info to Tableau |
| 32 | + Promise.all([tables, standardConnections]).then(function(data) { |
| 33 | + schemaCallback(data[0], data[1]); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + myConnector.getData = function(table, doneCallback) { |
| 38 | + // Load our data from the API. Multiple tables for WDC work by calling getData multiple times with a different id |
| 39 | + // so we want to make sure we are getting the correct table data per getData call |
| 40 | + loadJSON(table.tableInfo.id, function(data) { |
| 41 | + var obj = JSON.parse(data); |
| 42 | + var tableData = []; |
| 43 | + // Iterate through the data and build our table |
| 44 | + for (var i = 0; i < obj.length; i++) { |
| 45 | + tableEntry = {}; |
| 46 | + var ref = obj[i]; |
| 47 | + // We can use this handy shortcut because our JSON column names match our schema's column names perfectly |
| 48 | + Object.getOwnPropertyNames(ref).forEach(function(val, idx, array){ |
| 49 | + // Handle specific cases by checking the name of the property |
| 50 | + switch(val) { |
| 51 | + case "address": |
| 52 | + tableEntry.lat = ref[val].geo.lat; |
| 53 | + tableEntry.lng = ref[val].geo.lng; |
| 54 | + tableEntry.zipcode = ref[val].zipcode; |
| 55 | + break; |
| 56 | + case "company": |
| 57 | + tableEntry.companyname = ref[val].name; |
| 58 | + tableEntry.catchPhrase = ref[val].catchPhrase; |
| 59 | + tableEntry.bs = ref[val].bs; |
| 60 | + break; |
| 61 | + default: |
| 62 | + tableEntry[val] = ref[val]; |
| 63 | + } |
| 64 | + }); |
| 65 | + tableData.push(tableEntry); |
| 66 | + } |
| 67 | + // Once we have all the data parsed, we send it to the Tableau table object |
| 68 | + table.appendRows(tableData); |
| 69 | + doneCallback(); |
| 70 | + }); |
| 71 | + } |
| 72 | + tableau.registerConnector(myConnector); |
| 73 | +})(); |
| 74 | + |
| 75 | + |
| 76 | +// Helper function that loads a json and a callback to call once that file is loaded |
| 77 | + |
| 78 | +function loadJSON(path, cb, isLocal) { |
| 79 | + var obj = new XMLHttpRequest(); |
| 80 | + obj.overrideMimeType("application/json"); |
| 81 | + if(isLocal) { |
| 82 | + obj.open("GET", "../json/" + path + ".json", true); |
| 83 | + } |
| 84 | + else { |
| 85 | + obj.open("GET", "https://crossorigin.me/http://jsonplaceholder.typicode.com/" + path, true); |
| 86 | + } |
| 87 | + obj.onreadystatechange = function() { |
| 88 | + if (obj.readyState == 4 && obj.status == "200"){ |
| 89 | + cb(obj.responseText); |
| 90 | + } |
| 91 | + } |
| 92 | + obj.send(null); |
| 93 | +} |
| 94 | + |
| 95 | +function send() { |
| 96 | + tableau.submit(); |
| 97 | +} |
0 commit comments