Refactored template loading

This commit is contained in:
Marcin Kurczewski 2014-10-05 10:41:12 +02:00
parent 688b5b1281
commit 784ff22050
18 changed files with 107 additions and 149 deletions

5
TODO
View file

@ -46,11 +46,6 @@ everything related to comments:
refactors: refactors:
- add enum validation in IValidatables (needs refactors of enums and - add enum validation in IValidatables (needs refactors of enums and
possible disposal of EnumHelper in favor of something more subtle) possible disposal of EnumHelper in favor of something more subtle)
- simplify template loading in presenters - right now template loading
requires _, util and promise:
promise.wait(util.loadTemplate('blah'))
.then(function(html) {
template = _.template(html); });
- change content spinner to nprogress: - change content spinner to nprogress:
http://ricostacruz.com/nprogress/ http://ricostacruz.com/nprogress/
- reduce dependencies - reduce dependencies

View file

@ -10,9 +10,7 @@ App.Presenters.GlobalCommentListPresenter = function(
topNavigationPresenter) { topNavigationPresenter) {
var $el; var $el;
var listTemplate; var templates = {};
var itemTemplate;
var postTemplate;
function init(args, loaded) { function init(args, loaded) {
$el = jQuery('#content'); $el = jQuery('#content');
@ -22,11 +20,11 @@ App.Presenters.GlobalCommentListPresenter = function(
util.promiseTemplate('global-comment-list'), util.promiseTemplate('global-comment-list'),
util.promiseTemplate('global-comment-list-item'), util.promiseTemplate('global-comment-list-item'),
util.promiseTemplate('post-list-item')) util.promiseTemplate('post-list-item'))
.then(function(listHtml, listItemHtml, postItemHtml) .then(function(listTemplate, listItemTemplate, postTemplate)
{ {
listTemplate = _.template(listHtml); templates.list = listTemplate;
itemTemplate = _.template(listItemHtml); templates.listItem = listItemTemplate;
postTemplate = _.template(postItemHtml); templates.post = postTemplate;
render(); render();
loaded(); loaded();
@ -66,7 +64,7 @@ App.Presenters.GlobalCommentListPresenter = function(
} }
function render() { function render() {
$el.html(listTemplate()); $el.html(templates.list());
} }
function renderPosts(data, clear) { function renderPosts(data, clear) {
@ -80,9 +78,9 @@ App.Presenters.GlobalCommentListPresenter = function(
var post = data.post; var post = data.post;
var comments = data.comments; var comments = data.comments;
var $post = jQuery('<li>' + itemTemplate({ var $post = jQuery('<li>' + templates.listItem({
post: post, post: post,
postTemplate: postTemplate, postTemplate: templates.post,
}) + '</li>'); }) + '</li>');
var presenter = App.DI.get('postCommentListPresenter'); var presenter = App.DI.get('postCommentListPresenter');

View file

@ -2,7 +2,6 @@ var App = App || {};
App.Presenters = App.Presenters || {}; App.Presenters = App.Presenters || {};
App.Presenters.HomePresenter = function( App.Presenters.HomePresenter = function(
_,
jQuery, jQuery,
util, util,
promise, promise,
@ -12,10 +11,9 @@ App.Presenters.HomePresenter = function(
messagePresenter) { messagePresenter) {
var $el = jQuery('#content'); var $el = jQuery('#content');
var homeTemplate; var templates = {};
var postContentTemplate;
var globals; var globals;
var post; var post;
function init(args, loaded) { function init(args, loaded) {
topNavigationPresenter.select('home'); topNavigationPresenter.select('home');
@ -27,12 +25,12 @@ App.Presenters.HomePresenter = function(
api.get('/globals'), api.get('/globals'),
api.get('/posts/featured')) api.get('/posts/featured'))
.then(function( .then(function(
homeTemplateHtml, homeTemplate,
postContentTemplateHtml, postContentTemplate,
globalsResponse, globalsResponse,
featuredPostResponse) { featuredPostResponse) {
homeTemplate = _.template(homeTemplateHtml); templates.home = homeTemplate;
postContentTemplate = _.template(postContentTemplateHtml); templates.postContent = postContentTemplate;
globals = globalsResponse.json; globals = globalsResponse.json;
post = featuredPostResponse.json; post = featuredPostResponse.json;
@ -46,9 +44,9 @@ App.Presenters.HomePresenter = function(
} }
function render() { function render() {
$el.html(homeTemplate({ $el.html(templates.home({
post: post, post: post,
postContentTemplate: postContentTemplate, 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 +63,4 @@ 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', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.HomePresenter);

View file

@ -2,7 +2,6 @@ var App = App || {};
App.Presenters = App.Presenters || {}; App.Presenters = App.Presenters || {};
App.Presenters.LoginPresenter = function( App.Presenters.LoginPresenter = function(
_,
jQuery, jQuery,
util, util,
promise, promise,
@ -13,7 +12,7 @@ App.Presenters.LoginPresenter = function(
var $el = jQuery('#content'); var $el = jQuery('#content');
var $messages; var $messages;
var template; var templates = {};
var previousRoute; var previousRoute;
function init(args, loaded) { function init(args, loaded) {
@ -21,8 +20,8 @@ App.Presenters.LoginPresenter = function(
topNavigationPresenter.changeTitle('Login'); topNavigationPresenter.changeTitle('Login');
previousRoute = args.previousRoute; previousRoute = args.previousRoute;
promise.wait(util.promiseTemplate('login-form')) promise.wait(util.promiseTemplate('login-form'))
.then(function(html) { .then(function(template) {
template = _.template(html); templates.login = template;
if (auth.isLoggedIn()) { if (auth.isLoggedIn()) {
finishLogin(); finishLogin();
} else { } else {
@ -34,7 +33,7 @@ App.Presenters.LoginPresenter = function(
} }
function render() { function render() {
$el.html(template()); $el.html(templates.login());
$el.find('form').submit(loginFormSubmitted); $el.find('form').submit(loginFormSubmitted);
$messages = $el.find('.messages'); $messages = $el.find('.messages');
$messages.width($el.find('form').width()); $messages.width($el.find('form').width());
@ -81,4 +80,4 @@ App.Presenters.LoginPresenter = function(
}; };
App.DI.register('loginPresenter', ['_', 'jQuery', 'util', 'promise', 'router', 'auth', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.LoginPresenter); App.DI.register('loginPresenter', ['jQuery', 'util', 'promise', 'router', 'auth', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.LoginPresenter);

View file

@ -20,7 +20,7 @@ App.Presenters.PagerPresenter = function(
var targetContent; var targetContent;
var endlessScroll = browsingSettings.getSettings().endlessScroll; var endlessScroll = browsingSettings.getSettings().endlessScroll;
var scrollInterval; var scrollInterval;
var template; var templates = {};
var forceClear = false; var forceClear = false;
var baseUri; var baseUri;
@ -39,8 +39,8 @@ App.Presenters.PagerPresenter = function(
pager.setSearchParams(args.searchParams); pager.setSearchParams(args.searchParams);
promise.wait(util.promiseTemplate('pager')) promise.wait(util.promiseTemplate('pager'))
.then(function(html) { .then(function(template) {
template = _.template(html); templates.pager = template;
render(); render();
loaded(); loaded();
}); });
@ -222,7 +222,7 @@ App.Presenters.PagerPresenter = function(
} }
function render() { function render() {
$target.html(template({originalHtml: targetContent})); $target.html(templates.pager({originalHtml: targetContent}));
$messages = $target.find('.pagination-content'); $messages = $target.find('.pagination-content');
$pageList = $target.find('.page-list'); $pageList = $target.find('.page-list');
if (endlessScroll) { if (endlessScroll) {

View file

@ -12,10 +12,8 @@ App.Presenters.PostCommentListPresenter = function(
messagePresenter) { messagePresenter) {
var $el; var $el;
var commentListTemplate;
var commentListItemTemplate;
var commentFormTemplate;
var privileges; var privileges;
var templates = {};
var post; var post;
var comments = []; var comments = [];
@ -40,14 +38,14 @@ App.Presenters.PostCommentListPresenter = function(
util.promiseTemplate('comment-form'), util.promiseTemplate('comment-form'),
comments.length === 0 ? api.get('/comments/' + args.post.id) : null) comments.length === 0 ? api.get('/comments/' + args.post.id) : null)
.then(function( .then(function(
commentListHtml, commentListTemplate,
commentListItemHtml, commentListItemTemplate,
commentFormHtml, commentFormTemplate,
commentsResponse) commentsResponse)
{ {
commentListTemplate = _.template(commentListHtml); templates.commentList = commentListTemplate;
commentListItemTemplate = _.template(commentListItemHtml); templates.commentListItem = commentListItemTemplate;
commentFormTemplate = _.template(commentFormHtml); templates.commentForm = commentFormTemplate;
if (commentsResponse) { if (commentsResponse) {
comments = commentsResponse.json.data; comments = commentsResponse.json.data;
} }
@ -59,11 +57,11 @@ App.Presenters.PostCommentListPresenter = function(
} }
function render() { function render() {
$el.html(commentListTemplate( $el.html(templates.commentList(
_.extend( _.extend(
{ {
commentListItemTemplate: commentListItemTemplate, commentListItemTemplate: templates.commentListItem,
commentFormTemplate: commentFormTemplate, commentFormTemplate: templates.commentForm,
formatRelativeTime: util.formatRelativeTime, formatRelativeTime: util.formatRelativeTime,
formatMarkdown: util.formatMarkdown, formatMarkdown: util.formatMarkdown,
comments: comments, comments: comments,
@ -95,7 +93,7 @@ App.Presenters.PostCommentListPresenter = function(
} }
function renderComment($targetList, comment) { function renderComment($targetList, comment) {
var $item = jQuery('<li>' + commentListItemTemplate({ var $item = jQuery('<li>' + templates.commentListItem({
comment: comment, comment: comment,
formatRelativeTime: util.formatRelativeTime, formatRelativeTime: util.formatRelativeTime,
formatMarkdown: util.formatMarkdown, formatMarkdown: util.formatMarkdown,
@ -167,7 +165,7 @@ App.Presenters.PostCommentListPresenter = function(
if ($item.find('.comment-form').length > 0) { if ($item.find('.comment-form').length > 0) {
return; return;
} }
var $form = jQuery(commentFormTemplate({title: 'Edit comment', text: comment.text})); var $form = jQuery(templates.commentForm({title: 'Edit comment', text: comment.text}));
$item.find('.body').append($form); $item.find('.body').append($form);
$item.find('form button[type=submit]').click(function(e) { commentFormSubmitted(e, comment); }); $item.find('form button[type=submit]').click(function(e) { commentFormSubmitted(e, comment); });
} }

View file

@ -13,10 +13,9 @@ App.Presenters.PostListPresenter = function(
var KEY_RETURN = 13; var KEY_RETURN = 13;
var templates = {};
var $el = jQuery('#content'); var $el = jQuery('#content');
var $searchInput; var $searchInput;
var listTemplate;
var itemTemplate;
var searchArgs; var searchArgs;
@ -29,9 +28,9 @@ App.Presenters.PostListPresenter = function(
promise.wait( promise.wait(
util.promiseTemplate('post-list'), util.promiseTemplate('post-list'),
util.promiseTemplate('post-list-item')) util.promiseTemplate('post-list-item'))
.then(function(listHtml, itemHtml) { .then(function(listTemplate, listItemTemplate) {
listTemplate = _.template(listHtml); templates.list = listTemplate;
itemTemplate = _.template(itemHtml); templates.listItem = listItemTemplate;
render(); render();
loaded(); loaded();
@ -69,7 +68,7 @@ App.Presenters.PostListPresenter = function(
} }
function render() { function render() {
$el.html(listTemplate()); $el.html(templates.list());
$searchInput = $el.find('input[name=query]'); $searchInput = $el.find('input[name=query]');
$searchInput.val(searchArgs.query); $searchInput.val(searchArgs.query);
@ -93,7 +92,7 @@ App.Presenters.PostListPresenter = function(
} }
_.each(posts, function(post) { _.each(posts, function(post) {
$target.append(jQuery('<li>' + itemTemplate({ $target.append(jQuery('<li>' + templates.listItem({
searchArgs: searchArgs, searchArgs: searchArgs,
post: post, post: post,
}) + '</li>')); }) + '</li>'));

View file

@ -19,18 +19,11 @@ App.Presenters.PostPresenter = function(
var $el = jQuery('#content'); var $el = jQuery('#content');
var $messages = $el; var $messages = $el;
var postTemplate; var templates = {};
var postEditTemplate;
var postContentTemplate;
var historyTemplate;
var postNameOrId; var postNameOrId;
var searchArgs; var searchArgs;
var post; var post;
var postScore;
var postFavorites;
var postHistory;
var privileges = {}; var privileges = {};
var editPrivileges = {}; var editPrivileges = {};
@ -61,14 +54,14 @@ App.Presenters.PostPresenter = function(
util.promiseTemplate('post-content'), util.promiseTemplate('post-content'),
util.promiseTemplate('history')) util.promiseTemplate('history'))
.then(function( .then(function(
postTemplateHtml, postTemplate,
postEditTemplateHtml, postEditTemplate,
postContentTemplateHtml, postContentTemplate,
historyTemplateHtml) { historyTemplate) {
postTemplate = _.template(postTemplateHtml); templates.post = postTemplate;
postEditTemplate = _.template(postEditTemplateHtml); templates.postEdit = postEditTemplate;
postContentTemplate = _.template(postContentTemplateHtml); templates.postContent = postContentTemplate;
historyTemplate = _.template(historyTemplateHtml); templates.history = historyTemplate;
reinit(args, loaded); reinit(args, loaded);
}).fail(function(response) { }).fail(function(response) {
@ -129,9 +122,6 @@ App.Presenters.PostPresenter = function(
promise.wait(api.get('/posts/' + postNameOrId)) promise.wait(api.get('/posts/' + postNameOrId))
.then(function(postResponse) { .then(function(postResponse) {
post = postResponse.json; post = postResponse.json;
postScore = postResponse.json.ownScore;
postFavorites = postResponse.json.favorites;
postHistory = postResponse.json.history;
resolve(); resolve();
}).fail(function(response) { }).fail(function(response) {
showGenericError(response); showGenericError(response);
@ -178,21 +168,21 @@ App.Presenters.PostPresenter = function(
} }
function renderPostTemplate() { function renderPostTemplate() {
return postTemplate({ return templates.post({
searchArgs: searchArgs, searchArgs: searchArgs,
post: post, post: post,
ownScore: postScore, ownScore: post.ownScore,
postFavorites: postFavorites, postFavorites: post.favorites,
postHistory: postHistory, postHistory: post.history,
formatRelativeTime: util.formatRelativeTime, formatRelativeTime: util.formatRelativeTime,
formatFileSize: util.formatFileSize, formatFileSize: util.formatFileSize,
postContentTemplate: postContentTemplate, postContentTemplate: templates.postContent,
postEditTemplate: postEditTemplate, postEditTemplate: templates.postEdit,
historyTemplate: historyTemplate, historyTemplate: templates.history,
hasFav: _.any(postFavorites, function(favUser) { return favUser.id === auth.getCurrentUser().id; }), hasFav: _.any(post.favorites, function(favUser) { return favUser.id === auth.getCurrentUser().id; }),
isLoggedIn: auth.isLoggedIn(), isLoggedIn: auth.isLoggedIn(),
privileges: privileges, privileges: privileges,
editPrivileges: editPrivileges, editPrivileges: editPrivileges,

View file

@ -17,7 +17,7 @@ App.Presenters.PostUploadPresenter = function(
var $el = jQuery('#content'); var $el = jQuery('#content');
var $messages; var $messages;
var template; var templates = {};
var allPosts = []; var allPosts = [];
var tagInput; var tagInput;
var fileDropper; var fileDropper;
@ -28,15 +28,15 @@ App.Presenters.PostUploadPresenter = function(
topNavigationPresenter.changeTitle('Upload'); topNavigationPresenter.changeTitle('Upload');
promise.wait(util.promiseTemplate('post-upload')) promise.wait(util.promiseTemplate('post-upload'))
.then(function(html) { .then(function(template) {
template = _.template(html); templates.upload = template;
render(); render();
loaded(); loaded();
}); });
} }
function render() { function render() {
$el.html(template({ $el.html(templates.upload({
canUploadPostsAnonymously: auth.hasPrivilege(auth.privileges.uploadPostsAnonymously) canUploadPostsAnonymously: auth.hasPrivilege(auth.privileges.uploadPostsAnonymously)
})); }));
$messages = $el.find('.messages'); $messages = $el.find('.messages');

View file

@ -2,7 +2,6 @@ var App = App || {};
App.Presenters = App.Presenters || {}; App.Presenters = App.Presenters || {};
App.Presenters.RegistrationPresenter = function( App.Presenters.RegistrationPresenter = function(
_,
jQuery, jQuery,
util, util,
promise, promise,
@ -11,22 +10,22 @@ App.Presenters.RegistrationPresenter = function(
messagePresenter) { messagePresenter) {
var $el = jQuery('#content'); var $el = jQuery('#content');
var template; var templates = {};
var $messages; var $messages;
function init(args, loaded) { function init(args, loaded) {
topNavigationPresenter.select('register'); topNavigationPresenter.select('register');
topNavigationPresenter.changeTitle('Registration'); topNavigationPresenter.changeTitle('Registration');
promise.wait(util.promiseTemplate('registration-form')) promise.wait(util.promiseTemplate('registration-form'))
.then(function(html) { .then(function(template) {
template = _.template(html); templates.registration = template;
render(); render();
loaded(); loaded();
}); });
} }
function render() { function render() {
$el.html(template()); $el.html(templates.registration());
$el.find('form').submit(registrationFormSubmitted); $el.find('form').submit(registrationFormSubmitted);
$messages = $el.find('.messages'); $messages = $el.find('.messages');
$messages.width($el.find('form').width()); $messages.width($el.find('form').width());
@ -97,4 +96,4 @@ App.Presenters.RegistrationPresenter = function(
}; };
App.DI.register('registrationPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.RegistrationPresenter); App.DI.register('registrationPresenter', ['jQuery', 'util', 'promise', 'api', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.RegistrationPresenter);

View file

@ -2,7 +2,6 @@ var App = App || {};
App.Presenters = App.Presenters || {}; App.Presenters = App.Presenters || {};
App.Presenters.TopNavigationPresenter = function( App.Presenters.TopNavigationPresenter = function(
_,
jQuery, jQuery,
util, util,
promise, promise,
@ -10,13 +9,13 @@ App.Presenters.TopNavigationPresenter = function(
var selectedElement = null; var selectedElement = null;
var $el = jQuery('#top-navigation'); var $el = jQuery('#top-navigation');
var template; var templates = {};
var baseTitle = document.title; var baseTitle = document.title;
function init(args, loaded) { function init(args, loaded) {
promise.wait(util.promiseTemplate('top-navigation')) promise.wait(util.promiseTemplate('top-navigation'))
.then(function(html) { .then(function(template) {
template = _.template(html); templates.topNavigation = template;
render(); render();
loaded(); loaded();
auth.startObservingLoginChanges('top-navigation', loginStateChanged); auth.startObservingLoginChanges('top-navigation', loginStateChanged);
@ -34,7 +33,7 @@ App.Presenters.TopNavigationPresenter = function(
} }
function render() { function render() {
$el.html(template({ $el.html(templates.topNavigation({
loggedIn: auth.isLoggedIn(), loggedIn: auth.isLoggedIn(),
user: auth.getCurrentUser(), user: auth.getCurrentUser(),
canListUsers: auth.hasPrivilege(auth.privileges.listUsers), canListUsers: auth.hasPrivilege(auth.privileges.listUsers),
@ -75,4 +74,4 @@ App.Presenters.TopNavigationPresenter = function(
}; };
App.DI.registerSingleton('topNavigationPresenter', ['_', 'jQuery', 'util', 'promise', 'auth'], App.Presenters.TopNavigationPresenter); App.DI.registerSingleton('topNavigationPresenter', ['jQuery', 'util', 'promise', 'auth'], App.Presenters.TopNavigationPresenter);

View file

@ -2,7 +2,6 @@ var App = App || {};
App.Presenters = App.Presenters || {}; App.Presenters = App.Presenters || {};
App.Presenters.UserAccountRemovalPresenter = function( App.Presenters.UserAccountRemovalPresenter = function(
_,
jQuery, jQuery,
util, util,
promise, promise,
@ -12,7 +11,7 @@ App.Presenters.UserAccountRemovalPresenter = function(
messagePresenter) { messagePresenter) {
var target; var target;
var template; var templates = {};
var user; var user;
var privileges = {}; var privileges = {};
@ -25,8 +24,8 @@ App.Presenters.UserAccountRemovalPresenter = function(
(auth.hasPrivilege(auth.privileges.deleteOwnAccount) && auth.isLoggedIn(user.name)); (auth.hasPrivilege(auth.privileges.deleteOwnAccount) && auth.isLoggedIn(user.name));
promise.wait(util.promiseTemplate('account-removal')) promise.wait(util.promiseTemplate('account-removal'))
.then(function(html) { .then(function(template) {
template = _.template(html); templates.accountRemoval = template;
render(); render();
loaded(); loaded();
}); });
@ -34,7 +33,7 @@ App.Presenters.UserAccountRemovalPresenter = function(
function render() { function render() {
var $el = jQuery(target); var $el = jQuery(target);
$el.html(template({ $el.html(templates.accountRemoval({
user: user, user: user,
canDeleteAccount: privileges.canDeleteAccount})); canDeleteAccount: privileges.canDeleteAccount}));
@ -77,4 +76,4 @@ App.Presenters.UserAccountRemovalPresenter = function(
}; };
App.DI.register('userAccountRemovalPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'auth', 'router', 'messagePresenter'], App.Presenters.UserAccountRemovalPresenter); App.DI.register('userAccountRemovalPresenter', ['jQuery', 'util', 'promise', 'api', 'auth', 'router', 'messagePresenter'], App.Presenters.UserAccountRemovalPresenter);

View file

@ -11,7 +11,7 @@ App.Presenters.UserAccountSettingsPresenter = function(
messagePresenter) { messagePresenter) {
var target; var target;
var template; var templates = {};
var user; var user;
var privileges; var privileges;
var avatarContent; var avatarContent;
@ -41,8 +41,8 @@ App.Presenters.UserAccountSettingsPresenter = function(
}; };
promise.wait(util.promiseTemplate('account-settings')) promise.wait(util.promiseTemplate('account-settings'))
.then(function(html) { .then(function(template) {
template = _.template(html); templates.accountRemoval = template;
render(); render();
loaded(); loaded();
}); });
@ -50,7 +50,7 @@ App.Presenters.UserAccountSettingsPresenter = function(
function render() { function render() {
var $el = jQuery(target); var $el = jQuery(target);
$el.html(template(_.extend({user: user}, privileges))); $el.html(templates.accountRemoval(_.extend({user: user}, privileges)));
$el.find('form').submit(accountSettingsFormSubmitted); $el.find('form').submit(accountSettingsFormSubmitted);
$el.find('form [name=avatar-style]').change(avatarStyleChanged); $el.find('form [name=avatar-style]').change(avatarStyleChanged);
avatarStyleChanged(); avatarStyleChanged();

View file

@ -2,7 +2,6 @@ var App = App || {};
App.Presenters = App.Presenters || {}; App.Presenters = App.Presenters || {};
App.Presenters.UserActivationPresenter = function( App.Presenters.UserActivationPresenter = function(
_,
jQuery, jQuery,
promise, promise,
util, util,
@ -14,7 +13,7 @@ App.Presenters.UserActivationPresenter = function(
var $el = jQuery('#content'); var $el = jQuery('#content');
var $messages = $el; var $messages = $el;
var template; var templates = {};
var formHidden = false; var formHidden = false;
var operation; var operation;
@ -27,8 +26,8 @@ App.Presenters.UserActivationPresenter = function(
function reinit(args, loaded) { function reinit(args, loaded) {
operation = args.operation; operation = args.operation;
promise.wait(util.promiseTemplate('user-query-form')) promise.wait(util.promiseTemplate('user-query-form'))
.then(function(html) { .then(function(template) {
template = _.template(html); templates.userQuery = template;
if (args.token) { if (args.token) {
hideForm(); hideForm();
confirmToken(args.token); confirmToken(args.token);
@ -41,7 +40,7 @@ App.Presenters.UserActivationPresenter = function(
} }
function render() { function render() {
$el.html(template()); $el.html(templates.userQuery());
$messages = $el.find('.messages'); $messages = $el.find('.messages');
if (formHidden) { if (formHidden) {
$el.find('form').hide(); $el.find('form').hide();
@ -114,4 +113,4 @@ App.Presenters.UserActivationPresenter = function(
}; };
App.DI.register('userActivationPresenter', ['_', 'jQuery', 'promise', 'util', 'auth', 'api', 'router', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.UserActivationPresenter); App.DI.register('userActivationPresenter', ['jQuery', 'promise', 'util', 'auth', 'api', 'router', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.UserActivationPresenter);

View file

@ -2,7 +2,6 @@ var App = App || {};
App.Presenters = App.Presenters || {}; App.Presenters = App.Presenters || {};
App.Presenters.UserBrowsingSettingsPresenter = function( App.Presenters.UserBrowsingSettingsPresenter = function(
_,
jQuery, jQuery,
util, util,
promise, promise,
@ -11,7 +10,7 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
messagePresenter) { messagePresenter) {
var target; var target;
var template; var templates = {};
var user; var user;
var privileges = {}; var privileges = {};
@ -22,8 +21,8 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
privileges.canChangeBrowsingSettings = auth.isLoggedIn(user.name) && user.name === auth.getCurrentUser().name; privileges.canChangeBrowsingSettings = auth.isLoggedIn(user.name) && user.name === auth.getCurrentUser().name;
promise.wait(util.promiseTemplate('browsing-settings')) promise.wait(util.promiseTemplate('browsing-settings'))
.then(function(html) { .then(function(template) {
template = _.template(html); templates.browsingSettings = template;
render(); render();
loaded(); loaded();
}); });
@ -31,7 +30,7 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
function render() { function render() {
var $el = jQuery(target); var $el = jQuery(target);
$el.html(template({user: user, settings: browsingSettings.getSettings()})); $el.html(templates.browsingSettings({user: user, settings: browsingSettings.getSettings()}));
$el.find('form').submit(browsingSettingsFormSubmitted); $el.find('form').submit(browsingSettingsFormSubmitted);
} }
@ -69,4 +68,4 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
}; };
App.DI.register('userBrowsingSettingsPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'browsingSettings', 'messagePresenter'], App.Presenters.UserBrowsingSettingsPresenter); App.DI.register('userBrowsingSettingsPresenter', ['jQuery', 'util', 'promise', 'auth', 'browsingSettings', 'messagePresenter'], App.Presenters.UserBrowsingSettingsPresenter);

View file

@ -11,8 +11,7 @@ App.Presenters.UserListPresenter = function(
topNavigationPresenter) { topNavigationPresenter) {
var $el = jQuery('#content'); var $el = jQuery('#content');
var listTemplate; var templates = {};
var itemTemplate;
function init(args, loaded) { function init(args, loaded) {
topNavigationPresenter.select('users'); topNavigationPresenter.select('users');
@ -21,9 +20,9 @@ App.Presenters.UserListPresenter = function(
promise.wait( promise.wait(
util.promiseTemplate('user-list'), util.promiseTemplate('user-list'),
util.promiseTemplate('user-list-item')) util.promiseTemplate('user-list-item'))
.then(function(listHtml, itemHtml) { .then(function(listTemplate, listItemTemplate) {
listTemplate = _.template(listHtml); templates.list = listTemplate;
itemTemplate = _.template(itemHtml); templates.listItem = listItemTemplate;
render(); render();
loaded(); loaded();
@ -61,7 +60,7 @@ App.Presenters.UserListPresenter = function(
} }
function render() { function render() {
$el.html(listTemplate()); $el.html(templates.list());
$el.find('.order a').click(orderLinkClicked); $el.find('.order a').click(orderLinkClicked);
} }
@ -78,7 +77,7 @@ App.Presenters.UserListPresenter = function(
} }
_.each(users, function(user) { _.each(users, function(user) {
$target.append(jQuery('<li>' + itemTemplate({ $target.append(jQuery('<li>' + templates.listItem({
user: user, user: user,
formatRelativeTime: util.formatRelativeTime, formatRelativeTime: util.formatRelativeTime,
}) + '</li>')); }) + '</li>'));

View file

@ -17,7 +17,7 @@ App.Presenters.UserPresenter = function(
var $el = jQuery('#content'); var $el = jQuery('#content');
var $messages = $el; var $messages = $el;
var template; var templates = {};
var user; var user;
var userName; var userName;
var activeTab; var activeTab;
@ -30,11 +30,9 @@ App.Presenters.UserPresenter = function(
promise.wait( promise.wait(
util.promiseTemplate('user'), util.promiseTemplate('user'),
api.get('/users/' + userName)) api.get('/users/' + userName))
.then(function( .then(function(template, response) {
userHtml,
response) {
$messages = $el.find('.messages'); $messages = $el.find('.messages');
template = _.template(userHtml); templates.user = template;
user = response.json; user = response.json;
var extendedContext = _.extend(args, {user: user}); var extendedContext = _.extend(args, {user: user});
@ -65,7 +63,7 @@ App.Presenters.UserPresenter = function(
} }
function render() { function render() {
$el.html(template({ $el.html(templates.user({
user: user, user: user,
isLoggedIn: auth.isLoggedIn(user.name), isLoggedIn: auth.isLoggedIn(user.name),
formatRelativeTime: util.formatRelativeTime, formatRelativeTime: util.formatRelativeTime,

View file

@ -2,7 +2,6 @@ var App = App || {};
App.Util = function(_, jQuery, promise) { App.Util = function(_, jQuery, promise) {
var templateCache = {};
var exitConfirmationEnabled = false; var exitConfirmationEnabled = false;
function transparentPixel() { function transparentPixel() {
@ -51,25 +50,15 @@ App.Util = function(_, jQuery, promise) {
} }
function promiseTemplate(templateName) { function promiseTemplate(templateName) {
return promiseTemplateFromCache(templateName) || return promiseTemplateFromDOM(templateName) ||
promiseTemplateFromDOM(templateName) ||
promiseTemplateWithAJAX(templateName); promiseTemplateWithAJAX(templateName);
} }
function promiseTemplateFromCache(templateName) {
if (templateName in templateCache) {
return promise.make(function(resolve, reject) {
resolve(templateCache[templateName]);
});
}
return null;
}
function promiseTemplateFromDOM(templateName) { function promiseTemplateFromDOM(templateName) {
var $template = jQuery('#' + templateName + '-template'); var $template = jQuery('#' + templateName + '-template');
if ($template.length) { if ($template.length) {
return promise.make(function(resolve, reject) { return promise.make(function(resolve, reject) {
resolve($template.html()); resolve(_.template($template.html()));
}); });
} }
return null; return null;
@ -84,7 +73,7 @@ App.Util = function(_, jQuery, promise) {
url: templateUrl, url: templateUrl,
method: 'GET', method: 'GET',
success: function(data, textStatus, xhr) { success: function(data, textStatus, xhr) {
resolve(data); resolve(_.template(data));
}, },
error: function(xhr, textStatus, errorThrown) { error: function(xhr, textStatus, errorThrown) {
console.log(new Error('Error while loading template ' + templateName + ': ' + errorThrown)); console.log(new Error('Error while loading template ' + templateName + ': ' + errorThrown));