From 527356bba28d073c2e0d94358952685b19439094 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 22 Oct 2014 10:42:15 +0200 Subject: [PATCH] Extracted post content presenter --- public_html/index.html | 1 + public_html/js/Presenters/HomePresenter.js | 22 +++++++--- .../js/Presenters/PostContentPresenter.js | 41 +++++++++++++++++++ public_html/js/Presenters/PostPresenter.js | 7 ++-- public_html/templates/home.tpl | 4 +- public_html/templates/post.tpl | 3 +- 6 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 public_html/js/Presenters/PostContentPresenter.js diff --git a/public_html/index.html b/public_html/index.html index d384fc9c..3f6c08b6 100644 --- a/public_html/index.html +++ b/public_html/index.html @@ -120,6 +120,7 @@ + diff --git a/public_html/js/Presenters/HomePresenter.js b/public_html/js/Presenters/HomePresenter.js index 6e1e1a95..792b581f 100644 --- a/public_html/js/Presenters/HomePresenter.js +++ b/public_html/js/Presenters/HomePresenter.js @@ -7,6 +7,8 @@ App.Presenters.HomePresenter = function( promise, api, auth, + presenterManager, + postContentPresenter, topNavigationPresenter, messagePresenter) { @@ -21,22 +23,23 @@ App.Presenters.HomePresenter = function( promise.wait( util.promiseTemplate('home'), - util.promiseTemplate('post-content'), api.get('/globals'), api.get('/posts/featured')) .then(function( homeTemplate, - postContentTemplate, globalsResponse, featuredPostResponse) { templates.home = homeTemplate; - templates.postContent = postContentTemplate; globals = globalsResponse.json; post = featuredPostResponse.json.id ? featuredPostResponse.json : null; render(); loaded(); + presenterManager.initPresenters([ + [postContentPresenter, {post: post, $target: $el.find('#post-content-target')}]], + function() {}); + }).fail(function(response) { messagePresenter.showError($el, response.json && response.json.error || response); loaded(); @@ -46,7 +49,6 @@ App.Presenters.HomePresenter = function( function render() { $el.html(templates.home({ post: post, - postContentTemplate: templates.postContent, globals: globals, title: topNavigationPresenter.getBaseTitle(), 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); diff --git a/public_html/js/Presenters/PostContentPresenter.js b/public_html/js/Presenters/PostContentPresenter.js new file mode 100644 index 00000000..648de5b3 --- /dev/null +++ b/public_html/js/Presenters/PostContentPresenter.js @@ -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); diff --git a/public_html/js/Presenters/PostPresenter.js b/public_html/js/Presenters/PostPresenter.js index e8323e30..c821e887 100644 --- a/public_html/js/Presenters/PostPresenter.js +++ b/public_html/js/Presenters/PostPresenter.js @@ -13,6 +13,7 @@ App.Presenters.PostPresenter = function( presenterManager, postsAroundCalculator, postEditPresenter, + postContentPresenter, postCommentListPresenter, topNavigationPresenter, messagePresenter) { @@ -38,14 +39,11 @@ App.Presenters.PostPresenter = function( promise.wait( util.promiseTemplate('post'), - util.promiseTemplate('post-content'), util.promiseTemplate('history')) .then(function( postTemplate, - postContentTemplate, historyTemplate) { templates.post = postTemplate; - templates.postContent = postContentTemplate; templates.history = historyTemplate; reinit(params, loaded); @@ -69,6 +67,7 @@ App.Presenters.PostPresenter = function( loaded(); presenterManager.initPresenters([ + [postContentPresenter, {post: post, $target: $el.find('#post-content-target')}], [postEditPresenter, {post: post, $target: $el.find('#post-edit-target'), updateCallback: postEdited}], [postCommentListPresenter, {post: post, $target: $el.find('#post-comments-target')}]], function() {}); @@ -165,7 +164,6 @@ App.Presenters.PostPresenter = function( formatRelativeTime: util.formatRelativeTime, formatFileSize: util.formatFileSize, - postContentTemplate: templates.postContent, historyTemplate: templates.history, hasFav: _.any(post.favorites, function(favUser) { return favUser.id === auth.getCurrentUser().id; }), @@ -326,6 +324,7 @@ App.DI.register('postPresenter', [ 'presenterManager', 'postsAroundCalculator', 'postEditPresenter', + 'postContentPresenter', 'postCommentListPresenter', 'topNavigationPresenter', 'messagePresenter'], diff --git a/public_html/templates/home.tpl b/public_html/templates/home.tpl index a05887c8..2a9c2cad 100644 --- a/public_html/templates/home.tpl +++ b/public_html/templates/home.tpl @@ -6,7 +6,9 @@ <% if (post && typeof(post) !== 'undefined') { %>
- <%= postContentTemplate({post: post}) %> +
+
+