diff --git a/src/mibew/js/source/default/server.js b/src/mibew/js/source/default/server.js index ab14956d..526320a5 100644 --- a/src/mibew/js/source/default/server.js +++ b/src/mibew/js/source/default/server.js @@ -41,6 +41,8 @@ // Pause before restarting updater using Server.restartUpdater // function (in seconds) reconnectPause: 1, + // Call on successful response + onReceiveResponse: function() {}, // Call on request timeout onTimeout: function() {}, // Call when transport error was caught @@ -371,6 +373,8 @@ * @private */ Mibew.Server.prototype.receiveResponse = function(data, textStatus, jqXHR) { + // Call hook on successful request + this.options.onReceiveResponse(); // Do not parse empty responses if (data == '') { this.updateAfter(this.options.requestsFrequency); diff --git a/src/mibew/js/source/default/utils.js b/src/mibew/js/source/default/utils.js index 223b8be0..c2b7d120 100644 --- a/src/mibew/js/source/default/utils.js +++ b/src/mibew/js/source/default/utils.js @@ -197,7 +197,7 @@ // Do not open alert if one already opened. return; } - vex.dialog.alert({message: sanitizeMessage(message)}); + return vex.dialog.alert({message: sanitizeMessage(message)}); }; /** @@ -230,4 +230,12 @@ }); }; + /** + * Close alert. + * @param {Object} alert instance. + */ + Mibew.Utils.closeAlert = function(alertInstance) { + var res = vex.close(alertInstance.id); + }; + })(Mibew, jQuery, _, vex, validator); \ No newline at end of file diff --git a/src/mibew/js/source/users/app.js b/src/mibew/js/source/users/app.js index f2f68329..bc181432 100644 --- a/src/mibew/js/source/users/app.js +++ b/src/mibew/js/source/users/app.js @@ -24,6 +24,12 @@ */ var badRequestsCount = 0; + /** + * Represent alert object for network problems notification + * @type Object + */ + var networkProblemsAlert = null; + /** * Increase badRequestsCount and show reconnect message if need. * Calls on every bad request. @@ -32,12 +38,24 @@ // Increase bad requests count badRequestsCount++; // Check if there is - if (badRequestsCount == 10) { - Mibew.Utils.alert(Mibew.Localization.trans('Network problems detected. Please refresh the page.')); + if (!networkProblemsAlert && (badRequestsCount == 10)) { + networkProblemsAlert = Mibew.Utils.alert(Mibew.Localization.trans('Network problems detected. Please refresh the page.')); badRequestsCount = 0; } }; + /** + * Reset badRequestsCount and close reconnect message if need. + * Calls on every successful request. + */ + var successfulResponseHandler = function() { + badRequestsCount = 0; + if (networkProblemsAlert) { + Mibew.Utils.closeAlert(networkProblemsAlert); + } + networkProblemsAlert = null; + } + // Create application instance var App = new Backbone.Marionette.Application(); @@ -61,7 +79,8 @@ { 'interactionType': MibewAPIUsersInteraction, onTimeout: requestErrorHandler, - onTransportError: requestErrorHandler + onTransportError: requestErrorHandler, + onReceiveResponse: successfulResponseHandler, }, options.server ));