Extracted post content presenter

This commit is contained in:
Marcin Kurczewski 2014-10-22 10:42:15 +02:00
parent 82d59b57e6
commit 527356bba2
6 changed files with 67 additions and 11 deletions

View file

@ -120,6 +120,7 @@
<script type="text/javascript" src="/js/Presenters/PostListPresenter.js"></script> <script type="text/javascript" src="/js/Presenters/PostListPresenter.js"></script>
<script type="text/javascript" src="/js/Presenters/PostUploadPresenter.js"></script> <script type="text/javascript" src="/js/Presenters/PostUploadPresenter.js"></script>
<script type="text/javascript" src="/js/Presenters/PostContentPresenter.js"></script>
<script type="text/javascript" src="/js/Presenters/PostEditPresenter.js"></script> <script type="text/javascript" src="/js/Presenters/PostEditPresenter.js"></script>
<script type="text/javascript" src="/js/Presenters/PostPresenter.js"></script> <script type="text/javascript" src="/js/Presenters/PostPresenter.js"></script>

View file

@ -7,6 +7,8 @@ App.Presenters.HomePresenter = function(
promise, promise,
api, api,
auth, auth,
presenterManager,
postContentPresenter,
topNavigationPresenter, topNavigationPresenter,
messagePresenter) { messagePresenter) {
@ -21,22 +23,23 @@ App.Presenters.HomePresenter = function(
promise.wait( promise.wait(
util.promiseTemplate('home'), util.promiseTemplate('home'),
util.promiseTemplate('post-content'),
api.get('/globals'), api.get('/globals'),
api.get('/posts/featured')) api.get('/posts/featured'))
.then(function( .then(function(
homeTemplate, homeTemplate,
postContentTemplate,
globalsResponse, globalsResponse,
featuredPostResponse) { featuredPostResponse) {
templates.home = homeTemplate; templates.home = homeTemplate;
templates.postContent = postContentTemplate;
globals = globalsResponse.json; globals = globalsResponse.json;
post = featuredPostResponse.json.id ? featuredPostResponse.json : null; post = featuredPostResponse.json.id ? featuredPostResponse.json : null;
render(); render();
loaded(); loaded();
presenterManager.initPresenters([
[postContentPresenter, {post: post, $target: $el.find('#post-content-target')}]],
function() {});
}).fail(function(response) { }).fail(function(response) {
messagePresenter.showError($el, response.json && response.json.error || response); messagePresenter.showError($el, response.json && response.json.error || response);
loaded(); loaded();
@ -46,7 +49,6 @@ App.Presenters.HomePresenter = function(
function render() { function render() {
$el.html(templates.home({ $el.html(templates.home({
post: post, post: post,
postContentTemplate: templates.postContent,
globals: globals, globals: globals,
title: topNavigationPresenter.getBaseTitle(), title: topNavigationPresenter.getBaseTitle(),
canViewUsers: auth.hasPrivilege(auth.privileges.viewUsers), canViewUsers: auth.hasPrivilege(auth.privileges.viewUsers),
@ -65,4 +67,14 @@ App.Presenters.HomePresenter = function(
}; };
App.DI.register('homePresenter', ['jQuery', 'util', 'promise', 'api', 'auth', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.HomePresenter); App.DI.register('homePresenter', [
'jQuery',
'util',
'promise',
'api',
'auth',
'presenterManager',
'postContentPresenter',
'topNavigationPresenter',
'messagePresenter'],
App.Presenters.HomePresenter);

View file

@ -0,0 +1,41 @@
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.PostContentPresenter = function(
util,
promise) {
var post;
var templates = {};
var $target;
function init(params, loaded) {
$target = params.$target;
post = params.post;
promise.wait(util.promiseTemplate('post-content'))
.then(function(postContentTemplate) {
templates.postContent = postContentTemplate;
render();
loaded();
}).fail(function() {
console.log(arguments);
loaded();
});
}
function render() {
$target.html(templates.postContent({post: post}));
}
return {
init: init,
render: render,
};
};
App.DI.register('postContentPresenter', [
'util',
'promise'],
App.Presenters.PostContentPresenter);

View file

@ -13,6 +13,7 @@ App.Presenters.PostPresenter = function(
presenterManager, presenterManager,
postsAroundCalculator, postsAroundCalculator,
postEditPresenter, postEditPresenter,
postContentPresenter,
postCommentListPresenter, postCommentListPresenter,
topNavigationPresenter, topNavigationPresenter,
messagePresenter) { messagePresenter) {
@ -38,14 +39,11 @@ App.Presenters.PostPresenter = function(
promise.wait( promise.wait(
util.promiseTemplate('post'), util.promiseTemplate('post'),
util.promiseTemplate('post-content'),
util.promiseTemplate('history')) util.promiseTemplate('history'))
.then(function( .then(function(
postTemplate, postTemplate,
postContentTemplate,
historyTemplate) { historyTemplate) {
templates.post = postTemplate; templates.post = postTemplate;
templates.postContent = postContentTemplate;
templates.history = historyTemplate; templates.history = historyTemplate;
reinit(params, loaded); reinit(params, loaded);
@ -69,6 +67,7 @@ App.Presenters.PostPresenter = function(
loaded(); loaded();
presenterManager.initPresenters([ presenterManager.initPresenters([
[postContentPresenter, {post: post, $target: $el.find('#post-content-target')}],
[postEditPresenter, {post: post, $target: $el.find('#post-edit-target'), updateCallback: postEdited}], [postEditPresenter, {post: post, $target: $el.find('#post-edit-target'), updateCallback: postEdited}],
[postCommentListPresenter, {post: post, $target: $el.find('#post-comments-target')}]], [postCommentListPresenter, {post: post, $target: $el.find('#post-comments-target')}]],
function() {}); function() {});
@ -165,7 +164,6 @@ App.Presenters.PostPresenter = function(
formatRelativeTime: util.formatRelativeTime, formatRelativeTime: util.formatRelativeTime,
formatFileSize: util.formatFileSize, formatFileSize: util.formatFileSize,
postContentTemplate: templates.postContent,
historyTemplate: templates.history, historyTemplate: templates.history,
hasFav: _.any(post.favorites, function(favUser) { return favUser.id === auth.getCurrentUser().id; }), hasFav: _.any(post.favorites, function(favUser) { return favUser.id === auth.getCurrentUser().id; }),
@ -326,6 +324,7 @@ App.DI.register('postPresenter', [
'presenterManager', 'presenterManager',
'postsAroundCalculator', 'postsAroundCalculator',
'postEditPresenter', 'postEditPresenter',
'postContentPresenter',
'postCommentListPresenter', 'postCommentListPresenter',
'topNavigationPresenter', 'topNavigationPresenter',
'messagePresenter'], 'messagePresenter'],

View file

@ -6,7 +6,9 @@
<% if (post && typeof(post) !== 'undefined') { %> <% if (post && typeof(post) !== 'undefined') { %>
<div class="post"> <div class="post">
<%= postContentTemplate({post: post}) %> <div id="post-content-target">
</div>
<div class="post-footer"> <div class="post-footer">
<span class="left"> <span class="left">

View file

@ -255,7 +255,8 @@
<div id="post-edit-target"> <div id="post-edit-target">
</div> </div>
<%= postContentTemplate({post: post}) %> <div id="post-content-target">
</div>
<% if (privileges.canViewHistory) { %> <% if (privileges.canViewHistory) { %>
<div class="post-history-wrapper"> <div class="post-history-wrapper">