2014-09-07 19:49:11 +02:00
|
|
|
var App = App || {};
|
|
|
|
App.Presenters = App.Presenters || {};
|
|
|
|
|
|
|
|
App.Presenters.PostListPresenter = function(
|
2014-09-17 18:34:57 +02:00
|
|
|
_,
|
2014-09-07 19:49:11 +02:00
|
|
|
jQuery,
|
2014-09-17 18:34:57 +02:00
|
|
|
util,
|
|
|
|
promise,
|
|
|
|
auth,
|
|
|
|
router,
|
|
|
|
pagedCollectionPresenter,
|
|
|
|
topNavigationPresenter,
|
|
|
|
messagePresenter) {
|
2014-09-07 19:49:11 +02:00
|
|
|
|
|
|
|
var $el = jQuery('#content');
|
2014-09-17 18:34:57 +02:00
|
|
|
var listTemplate;
|
|
|
|
var itemTemplate;
|
2014-09-07 19:49:11 +02:00
|
|
|
|
2014-09-17 22:37:49 +02:00
|
|
|
function init(args, loaded) {
|
2014-09-07 19:49:11 +02:00
|
|
|
topNavigationPresenter.select('posts');
|
2014-09-11 13:59:12 +02:00
|
|
|
topNavigationPresenter.changeTitle('Posts');
|
2014-09-17 18:34:57 +02:00
|
|
|
|
|
|
|
promise.waitAll(
|
2014-09-17 22:37:49 +02:00
|
|
|
util.promiseTemplate('post-list'),
|
|
|
|
util.promiseTemplate('post-list-item'))
|
|
|
|
.then(function(listHtml, itemHtml) {
|
|
|
|
listTemplate = _.template(listHtml);
|
|
|
|
itemTemplate = _.template(itemHtml);
|
|
|
|
|
|
|
|
render();
|
2014-09-18 11:29:19 +02:00
|
|
|
loaded();
|
|
|
|
|
|
|
|
pagedCollectionPresenter.init({
|
|
|
|
baseUri: '#/posts',
|
|
|
|
backendUri: '/posts',
|
|
|
|
$target: $el.find('.pagination-target'),
|
|
|
|
updateCallback: function(data, clear) {
|
|
|
|
renderPosts(data.entities, clear);
|
|
|
|
},
|
|
|
|
failCallback: function(response) {
|
|
|
|
$el.empty();
|
|
|
|
messagePresenter.showError($el, response.json && response.json.error || response);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
reinit(args, function() {});
|
|
|
|
});
|
2014-09-17 22:37:49 +02:00
|
|
|
});
|
2014-09-17 18:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 22:37:49 +02:00
|
|
|
function reinit(args, loaded) {
|
2014-09-18 11:29:19 +02:00
|
|
|
loaded();
|
2014-09-17 18:34:57 +02:00
|
|
|
|
2014-09-18 11:29:19 +02:00
|
|
|
var searchArgs = util.parseComplexRouteArgs(args.searchArgs);
|
|
|
|
pagedCollectionPresenter.reinit({page: searchArgs.page, searchParams: {order: searchArgs.order}});
|
2014-09-07 19:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function render() {
|
2014-09-17 18:34:57 +02:00
|
|
|
$el.html(listTemplate());
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderPosts(posts, clear) {
|
|
|
|
var $target = $el.find('.posts');
|
|
|
|
|
|
|
|
var all = '';
|
|
|
|
_.each(posts, function(post) {
|
|
|
|
all += itemTemplate({
|
|
|
|
post: post,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (clear) {
|
|
|
|
$target.html(all);
|
|
|
|
} else {
|
|
|
|
$target.append(all);
|
|
|
|
}
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-09-07 19:49:11 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
init: init,
|
2014-09-17 18:34:57 +02:00
|
|
|
reinit: reinit,
|
2014-09-07 19:49:11 +02:00
|
|
|
render: render,
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-09-17 18:34:57 +02:00
|
|
|
App.DI.register('postListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'router', 'pagedCollectionPresenter', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.PostListPresenter);
|