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 || {};
App.Presenters = App.Presenters || {};
App.Presenters.MessagePresenter = function(jQuery) {
App.Presenters.MessagePresenter = function(_, jQuery) {
var options = {
instant: false
};
function showInfo($el, message) {
return showMessage($el, 'info', message);
@ -12,11 +16,16 @@ App.Presenters.MessagePresenter = function(jQuery) {
}
function hideMessages($el) {
$el.children('.message').each(function() {
jQuery(this).slideUp('fast', function() {
jQuery(this).remove();
var $messages = $el.children('.message');
if (options.instant) {
$messages.each(function() {
jQuery(this).slideUp('fast', function() {
jQuery(this).remove();
});
});
});
} else {
$messages.remove();
}
}
function showMessage($el, className, message) {
@ -24,18 +33,22 @@ App.Presenters.MessagePresenter = function(jQuery) {
$messageDiv.addClass('message');
$messageDiv.addClass(className);
$messageDiv.html(message);
$messageDiv.hide();
if (!options.instant) {
$messageDiv.hide();
}
$el.append($messageDiv);
$messageDiv.slideDown('fast');
if (!options.instant) {
$messageDiv.slideDown('fast');
}
return $messageDiv;
}
return {
return _.extend(options, {
showInfo: showInfo,
showError: showError,
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,
pager,
presenterManager,
messagePresenter,
browsingSettings) {
var $target;
var $pageList;
var $messages;
var targetContent;
var endlessScroll = browsingSettings.getSettings().endlessScroll;
var scrollInterval;
@ -23,12 +25,12 @@ App.Presenters.PagerPresenter = function(
var baseUri;
var updateCallback;
var failCallback;
function init(args, loaded) {
baseUri = args.baseUri;
updateCallback = args.updateCallback;
failCallback = args.failCallback;
messagePresenter.instant = true;
$target = args.$target;
targetContent = jQuery(args.$target).html();
@ -120,11 +122,13 @@ App.Presenters.PagerPresenter = function(
}
function retrieve() {
messagePresenter.hideMessages($messages);
showSpinner();
return promise.make(function(resolve, reject) {
pager.retrieve()
.then(function(response) {
showPageList();
updateCallback(response, forceClear || !endlessScroll);
forceClear = false;
@ -133,11 +137,11 @@ App.Presenters.PagerPresenter = function(
attachNextPageLoader();
resolve();
}).fail(function(response) {
if (typeof(failCallback) !== 'undefined') {
failCallback(response);
} else {
console.log(new Error(response.json && response.json.error || response));
}
clearContent();
hidePageList();
hideSpinner();
messagePresenter.showError($messages, response.json && response.json.error || response);
reject();
});
});
@ -163,6 +167,14 @@ App.Presenters.PagerPresenter = function(
}, 100);
}
function showPageList() {
$pageList.show();
}
function hidePageList() {
$pageList.hide();
}
function refreshPageList() {
var pages = pager.getVisiblePages();
$pageList.empty();
@ -191,6 +203,7 @@ App.Presenters.PagerPresenter = function(
function render() {
$target.html(template({originalHtml: targetContent}));
$messages = $target.find('.pagination-content');
$pageList = $target.find('.page-list');
if (endlessScroll) {
$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,
keyboard,
pagerPresenter,
topNavigationPresenter,
messagePresenter) {
topNavigationPresenter) {
var KEY_RETURN = 13;
@ -43,11 +42,6 @@ App.Presenters.PostListPresenter = function(
updateCallback: function(data, clear) {
renderPosts(data.entities, clear);
},
failCallback: function(response) {
$el.empty();
messagePresenter.showError($el, response.json && response.json.error || response);
loaded();
}
},
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,
auth,
pagerPresenter,
topNavigationPresenter,
messagePresenter) {
topNavigationPresenter) {
var $el = jQuery('#content');
var listTemplate;
@ -36,11 +35,6 @@ App.Presenters.UserListPresenter = function(
updateCallback: function(data, clear) {
renderUsers(data.entities, clear);
},
failCallback: function(response) {
$el.empty();
messagePresenter.showError($el, response.json && response.json.error || response);
loaded();
}
},
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);