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