From 34f03d58dfb501d140196fae7149b0fc33999ca8 Mon Sep 17 00:00:00 2001 From: gine Date: Sun, 26 Mar 2017 19:03:01 +0200 Subject: [PATCH] Aggiunto loader. Fix #20 --- www/js/crud.events.js | 41 +++++++++++++++++++++++++++++++---------- www/js/main.js | 4 +++- www/js/query.js | 3 +++ www/js/update.js | 2 ++ 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/www/js/crud.events.js b/www/js/crud.events.js index ea94347..1dbc97e 100644 --- a/www/js/crud.events.js +++ b/www/js/crud.events.js @@ -25,7 +25,14 @@ EVENTS.formatDate = function(date, format) { return mdate.isValid() ? mdate.format(format) : ""; }; +EVENTS.eventsLoaded = 0; + EVENTS.render = function() { + EVENTS.eventsLoaded = 0; + setTimeout(function () { + $.mobile.loading('show'); + }, 300); + var ret = checkConnection('ERR_EVENT'); if (ret) { EVENTS.getDataAndLoad(ai1ec.cats.single); @@ -40,7 +47,6 @@ EVENTS.getDataAndLoad = function (type) { type = type || ai1ec.cats.single; var table = type == ai1ec.cats.repeat ? 'xm24_events_repeat' : 'xm24_events_single'; - $.mobile.loading('show'); ai1ec.remote.getLastEvents(type, function(events) { var n_evs = events.length, n_evs_ins = 0, @@ -78,23 +84,38 @@ EVENTS.getDataAndLoad = function (type) { if(n_evs_ins > 0) { EVENTS.loadFromDb('#mostralista-'+ type, type); } else { - toastr.info("Non ci sono nuovi eventi."); - $.mobile.loading('hide'); + EVENTS.errorHandler("Non ci sono nuovi eventi.", "info"); } } console.error("EVENTS.query() : ", error); }); } }, function(error){ - console.error("EVENTS.query() : ", error + " type: " + type); - toastr.error("Err: errore nel TRUNCATE della tabella degli eventi"); + EVENTS.errorHandler("Err: errore nel TRUNCATE della tabella degli eventi"); }); } else { - toastr.info("Non ci sono nuovi eventi."); + EVENTS.errorHandler("Non ci sono nuovi eventi.", "info"); } }); }; +EVENTS.hideLoader = function(){ + if(EVENTS.eventsLoaded >= Object.keys(EVENTS.table).length) + $.mobile.loading('hide'); +}; + +EVENTS.errorHandler = function(msg, type) { + EVENTS.eventsLoaded++; + + if(type === 'info') { + toastr.info(msg); + } else { + toastr.error(msg); + } + + EVENTS.hideLoader(); +}; + EVENTS.loadFromDb = function (containerId, type) { var table = type == ai1ec.cats.repeat ? 'xm24_events_repeat' : 'xm24_events_single'; FONTE.dbHandler.executeSql("SELECT * FROM " + table + " ORDER BY initDate COLLATE NOCASE ASC;", [], function (resultSet) { @@ -123,14 +144,14 @@ EVENTS.loadFromDb = function (containerId, type) { items.push(li); } + EVENTS.eventsLoaded++; $(containerId).empty(); $("