From 627182f742f18d4af270764b24b01724aa16d381 Mon Sep 17 00:00:00 2001 From: Jon Ziebell Date: Wed, 14 Aug 2019 21:46:39 -0400 Subject: [PATCH] A bit more cleanup for #148 Requiring at least one API call to call .send(), adding error message if responseText is null, fixing settings attempting to send empty API call if no settings changed. --- js/beestat/api.js | 12 ++++++++++-- js/beestat/setting.js | 12 +++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/js/beestat/api.js b/js/beestat/api.js index 36b761a..9ae7d3f 100644 --- a/js/beestat/api.js +++ b/js/beestat/api.js @@ -48,6 +48,10 @@ beestat.api.prototype.send = function(opt_api_call) { this.xhr_.open('POST', '../api/?' + query_string); this.xhr_.send(); } else { + if (this.api_calls_.length === 0) { + throw new Error('Must add at least one API call.'); + } + if (this.is_batch_() === true) { // Only run uncached API calls. var uncached_batch_api_calls = []; @@ -80,7 +84,6 @@ beestat.api.prototype.send = function(opt_api_call) { } } else { var single_api_call = this.api_calls_[0]; - var cached = this.get_cached_(single_api_call); if (cached !== undefined) { if (this.callback_ !== undefined) { @@ -152,7 +155,12 @@ beestat.api.prototype.load_ = function(response_text) { try { response = window.JSON.parse(response_text); } catch (e) { - beestat.error('API returned invalid response.', response_text); + var detail = response_text; + if (detail === '') { + detail = this.xhr_.status + ' ' + this.xhr_.statusText; + } + + beestat.error('API returned invalid response.', detail); return; } diff --git a/js/beestat/setting.js b/js/beestat/setting.js index 165b160..9068239 100644 --- a/js/beestat/setting.js +++ b/js/beestat/setting.js @@ -9,6 +9,9 @@ * otherwise. */ beestat.setting = function(key, opt_value, opt_callback) { + if(opt_value !== undefined) { + console.log(arguments); + } var user = beestat.get_user(); var defaults = { @@ -59,6 +62,8 @@ beestat.setting = function(key, opt_value, opt_callback) { var api = new beestat.api(); api.set_callback(opt_callback); + var has_calls = false; + for (var k in settings) { if (user.json_settings[k] !== settings[k]) { user.json_settings[k] = settings[k]; @@ -73,8 +78,13 @@ beestat.setting = function(key, opt_value, opt_callback) { 'value': settings[k] } ); + + has_calls = true; } } - api.send(); + // If no settings changed no API call needs to be fired. + if (has_calls === true) { + api.send(); + } };