Improved error handling in pager presenters

This commit is contained in:
Marcin Kurczewski 2014-10-01 23:43:39 +02:00
parent 52d44284fb
commit ec8daf91fb
4 changed files with 48 additions and 34 deletions

View file

@ -1,7 +1,11 @@
var App = App || {}; var App = App || {};
App.Presenters = App.Presenters || {}; App.Presenters = App.Presenters || {};
App.Presenters.MessagePresenter = function(jQuery) { App.Presenters.MessagePresenter = function(_, jQuery) {
var options = {
instant: false
};
function showInfo($el, message) { function showInfo($el, message) {
return showMessage($el, 'info', message); return showMessage($el, 'info', message);
@ -12,11 +16,16 @@ App.Presenters.MessagePresenter = function(jQuery) {
} }
function hideMessages($el) { function hideMessages($el) {
$el.children('.message').each(function() { var $messages = $el.children('.message');
if (options.instant) {
$messages.each(function() {
jQuery(this).slideUp('fast', function() { jQuery(this).slideUp('fast', function() {
jQuery(this).remove(); jQuery(this).remove();
}); });
}); });
} else {
$messages.remove();
}
} }
function showMessage($el, className, message) { function showMessage($el, className, message) {
@ -24,18 +33,22 @@ App.Presenters.MessagePresenter = function(jQuery) {
$messageDiv.addClass('message'); $messageDiv.addClass('message');
$messageDiv.addClass(className); $messageDiv.addClass(className);
$messageDiv.html(message); $messageDiv.html(message);
if (!options.instant) {
$messageDiv.hide(); $messageDiv.hide();
}
$el.append($messageDiv); $el.append($messageDiv);
if (!options.instant) {
$messageDiv.slideDown('fast'); $messageDiv.slideDown('fast');
}
return $messageDiv; return $messageDiv;
} }
return { return _.extend(options, {
showInfo: showInfo, showInfo: showInfo,
showError: showError, showError: showError,
hideMessages: hideMessages, hideMessages: hideMessages,
}; });
}; };
App.DI.register('messagePresenter', ['jQuery'], App.Presenters.MessagePresenter); App.DI.register('messagePresenter', ['_', 'jQuery'], App.Presenters.MessagePresenter);

View file

@ -11,10 +11,12 @@ App.Presenters.PagerPresenter = function(
router, router,
pager, pager,
presenterManager, presenterManager,
messagePresenter,
browsingSettings) { browsingSettings) {
var $target; var $target;
var $pageList; var $pageList;
var $messages;
var targetContent; var targetContent;
var endlessScroll = browsingSettings.getSettings().endlessScroll; var endlessScroll = browsingSettings.getSettings().endlessScroll;
var scrollInterval; var scrollInterval;
@ -23,12 +25,12 @@ App.Presenters.PagerPresenter = function(
var baseUri; var baseUri;
var updateCallback; var updateCallback;
var failCallback;
function init(args, loaded) { function init(args, loaded) {
baseUri = args.baseUri; baseUri = args.baseUri;
updateCallback = args.updateCallback; updateCallback = args.updateCallback;
failCallback = args.failCallback;
messagePresenter.instant = true;
$target = args.$target; $target = args.$target;
targetContent = jQuery(args.$target).html(); targetContent = jQuery(args.$target).html();
@ -120,11 +122,13 @@ App.Presenters.PagerPresenter = function(
} }
function retrieve() { function retrieve() {
messagePresenter.hideMessages($messages);
showSpinner(); showSpinner();
return promise.make(function(resolve, reject) { return promise.make(function(resolve, reject) {
pager.retrieve() pager.retrieve()
.then(function(response) { .then(function(response) {
showPageList();
updateCallback(response, forceClear || !endlessScroll); updateCallback(response, forceClear || !endlessScroll);
forceClear = false; forceClear = false;
@ -133,11 +137,11 @@ App.Presenters.PagerPresenter = function(
attachNextPageLoader(); attachNextPageLoader();
resolve(); resolve();
}).fail(function(response) { }).fail(function(response) {
if (typeof(failCallback) !== 'undefined') { clearContent();
failCallback(response); hidePageList();
} else { hideSpinner();
console.log(new Error(response.json && response.json.error || response)); messagePresenter.showError($messages, response.json && response.json.error || response);
}
reject(); reject();
}); });
}); });
@ -163,6 +167,14 @@ App.Presenters.PagerPresenter = function(
}, 100); }, 100);
} }
function showPageList() {
$pageList.show();
}
function hidePageList() {
$pageList.hide();
}
function refreshPageList() { function refreshPageList() {
var pages = pager.getVisiblePages(); var pages = pager.getVisiblePages();
$pageList.empty(); $pageList.empty();
@ -191,6 +203,7 @@ App.Presenters.PagerPresenter = function(
function render() { function render() {
$target.html(template({originalHtml: targetContent})); $target.html(template({originalHtml: targetContent}));
$messages = $target.find('.pagination-content');
$pageList = $target.find('.page-list'); $pageList = $target.find('.page-list');
if (endlessScroll) { if (endlessScroll) {
$pageList.remove(); $pageList.remove();
@ -208,4 +221,4 @@ App.Presenters.PagerPresenter = function(
}; };
App.DI.register('pagerPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'keyboard', 'router', 'pager', 'presenterManager', 'browsingSettings'], App.Presenters.PagerPresenter); App.DI.register('pagerPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'keyboard', 'router', 'pager', 'presenterManager', 'messagePresenter', 'browsingSettings'], App.Presenters.PagerPresenter);

View file

@ -9,8 +9,7 @@ App.Presenters.PostListPresenter = function(
auth, auth,
keyboard, keyboard,
pagerPresenter, pagerPresenter,
topNavigationPresenter, topNavigationPresenter) {
messagePresenter) {
var KEY_RETURN = 13; var KEY_RETURN = 13;
@ -43,11 +42,6 @@ App.Presenters.PostListPresenter = function(
updateCallback: function(data, clear) { updateCallback: function(data, clear) {
renderPosts(data.entities, clear); renderPosts(data.entities, clear);
}, },
failCallback: function(response) {
$el.empty();
messagePresenter.showError($el, response.json && response.json.error || response);
loaded();
}
}, },
function() { function() {
reinit(args, function() {}); reinit(args, function() {});
@ -118,4 +112,4 @@ App.Presenters.PostListPresenter = function(
}; };
App.DI.register('postListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'keyboard', 'pagerPresenter', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.PostListPresenter); App.DI.register('postListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'keyboard', 'pagerPresenter', 'topNavigationPresenter'], App.Presenters.PostListPresenter);

View file

@ -8,8 +8,7 @@ App.Presenters.UserListPresenter = function(
promise, promise,
auth, auth,
pagerPresenter, pagerPresenter,
topNavigationPresenter, topNavigationPresenter) {
messagePresenter) {
var $el = jQuery('#content'); var $el = jQuery('#content');
var listTemplate; var listTemplate;
@ -36,11 +35,6 @@ App.Presenters.UserListPresenter = function(
updateCallback: function(data, clear) { updateCallback: function(data, clear) {
renderUsers(data.entities, clear); renderUsers(data.entities, clear);
}, },
failCallback: function(response) {
$el.empty();
messagePresenter.showError($el, response.json && response.json.error || response);
loaded();
}
}, },
function() { function() {
reinit(args, function() {}); reinit(args, function() {});
@ -104,4 +98,4 @@ App.Presenters.UserListPresenter = function(
}; };
App.DI.register('userListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'pagerPresenter', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.UserListPresenter); App.DI.register('userListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'pagerPresenter', 'topNavigationPresenter'], App.Presenters.UserListPresenter);