1
0
mirror of https://github.com/beestat/app.git synced 2025-07-09 03:04:07 -04:00

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.
This commit is contained in:
Jon Ziebell 2019-08-14 21:46:39 -04:00
parent 3a65f3afb2
commit 627182f742
2 changed files with 21 additions and 3 deletions

View File

@ -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;
}

View File

@ -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();
}
};