From 44387fa22976d7a169f3f76514d249ce7d4c0dc2 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Thu, 16 Oct 2014 16:58:32 -0500 Subject: [PATCH] Refactor javascript code to consume api --- static/js/app.js | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/static/js/app.js b/static/js/app.js index f707bae..c70c82f 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -1,18 +1,9 @@ -function apiCall(path, cb) { - $.getJSON(path, function(resp) { cb(resp); }); -} - -function getTables(cb) { apiCall("/tables", cb); } -function getTableStructure(table, cb) { apiCall("/tables/" + table, cb); } -function getTableIndexes(table, cb) { apiCall("/tables/" + table + "/indexes", cb); } -function getHistory(cb) { apiCall("/history", cb); } - -function executeQuery(query, cb) { +function apiCall(method, path, params, cb) { $.ajax({ - url: "/query", - method: "post", + url: path, + method: method, cache: false, - data: { query: query, format: "json" }, + data: params, success: function(data) { cb(data); }, @@ -22,6 +13,19 @@ function executeQuery(query, cb) { }); } +function getTables(cb) { apiCall("get", "/tables", {}, cb); } +function getTableStructure(table, cb) { apiCall("get", "/tables/" + table, {}, cb); } +function getTableIndexes(table, cb) { apiCall("get", "/tables/" + table + "/indexes", {}, cb); } +function getHistory(cb) { apiCall("get", "/history", {}, cb); } + +function executeQuery(query, cb) { + apiCall("post", "/query", { query: query }, cb); +} + +function explainQuery(query, cb) { + apiCall("post", "/explain", { query: query }, cb); +} + function loadTables() { getTables(function(data) { data.forEach(function(item) { @@ -150,6 +154,9 @@ function runQuery() { var query = $.trim(editor.getValue()); if (query.length == 0) { + $("#run").removeAttr("disabled"); + $("#explain").removeAttr("disabled"); + $("#query_progress").hide(); return; } @@ -174,12 +181,13 @@ function runExplain() { var query = $.trim(editor.getValue()); if (query.length == 0) { + $("#run").removeAttr("disabled"); + $("#explain").removeAttr("disabled"); + $("#query_progress").hide(); return; } - query = "EXPLAIN ANALYZE " + query; - - executeQuery(query, function(data) { + explainQuery(query, function(data) { buildTable(data); $("#run").removeAttr("disabled");