Restructured presenters code

This commit is contained in:
Marcin Kurczewski 2014-09-02 23:07:50 +02:00
parent aed090da7d
commit 7c8e473e1b
5 changed files with 98 additions and 91 deletions

View file

@ -18,12 +18,24 @@ App.Presenters.LoginPresenter = function(
util.loadTemplate('login-form').then(function(html) { util.loadTemplate('login-form').then(function(html) {
template = _.template(html); template = _.template(html);
render(); init();
}); });
var eventHandlers = { function init() {
if (appState.get('loggedIn'))
router.navigateToMainPage();
else
render();
}
loginFormSubmit: function(e) { function render() {
$el.html(template());
$el.find('form').submit(loginFormSubmitted);
$messages = $el.find('.messages');
$messages.width($el.find('form').width());
};
function loginFormSubmitted(e) {
e.preventDefault(); e.preventDefault();
messagePresenter.hideMessages($messages); messagePresenter.hideMessages($messages);
@ -40,19 +52,7 @@ App.Presenters.LoginPresenter = function(
}).catch(function(response) { }).catch(function(response) {
messagePresenter.showError($messages, response.json && response.json.error || response); messagePresenter.showError($messages, response.json && response.json.error || response);
}); });
}, }
};
if (appState.get('loggedIn'))
router.navigateToMainPage();
function render() {
$el.html(template());
$el.find('form').submit(eventHandlers.loginFormSubmit);
$messages = $el.find('.messages');
$messages.width($el.find('form').width());
};
return { return {
render: render, render: render,

View file

@ -12,22 +12,23 @@ App.Presenters.LogoutPresenter = function(
var $messages = jQuery('#content'); var $messages = jQuery('#content');
var eventHandlers = { init();
mainPageLinkClick: function(e) {
e.preventDefault();
router.navigateToMainPage();
},
};
function init() {
auth.logout().then(function() { auth.logout().then(function() {
var $messageDiv = messagePresenter.showInfo($messages, 'Logged out. <a href="">Back to main page</a>'); var $messageDiv = messagePresenter.showInfo($messages, 'Logged out. <a href="">Back to main page</a>');
$messageDiv.find('a').click(eventHandlers.mainPageLinkClick); $messageDiv.find('a').click(mainPageLinkClicked);
}).catch(function(response) { }).catch(function(response) {
messagePresenter.showError($messages, response.json && response.json.error || response); messagePresenter.showError($messages, response.json && response.json.error || response);
}); });
}
return { function mainPageLinkClicked(e) {
}; e.preventDefault();
router.navigateToMainPage();
}
return {};
}; };

View file

@ -15,12 +15,21 @@ App.Presenters.RegistrationPresenter = function(
util.loadTemplate('registration-form').then(function(html) { util.loadTemplate('registration-form').then(function(html) {
template = _.template(html); template = _.template(html);
render(); init();
}); });
var eventHandlers = { function init() {
render();
}
registrationFormSubmit: function(e) { function render() {
$el.html(template());
$el.find('form').submit(registrationFormSubmitted);
$messages = $el.find('.messages');
$messages.width($el.find('form').width());
}
function registrationFormSubmitted(e) {
e.preventDefault(); e.preventDefault();
messagePresenter.hideMessages($messages); messagePresenter.hideMessages($messages);
@ -36,32 +45,24 @@ App.Presenters.RegistrationPresenter = function(
api.post('/users', registrationData) api.post('/users', registrationData)
.then(function(response) { .then(function(response) {
eventHandlers.registrationSuccess(response); registrationSuccess(response);
}).catch(function(response) { }).catch(function(response) {
eventHandlers.registrationFailure(response); registrationFailure(response);
}); });
}, }
registrationSuccess: function(apiResponse) { function registrationSuccess(apiResponse) {
//todo: tell user if it turned out that he needs to confirm his e-mail //todo: tell user if it turned out that he needs to confirm his e-mail
$el.find('form').slideUp(function() { $el.find('form').slideUp(function() {
var message = 'Registration complete! '; var message = 'Registration complete! ';
message += '<a href="#/login">Click here</a> to login.'; message += '<a href="#/login">Click here</a> to login.';
messagePresenter.showInfo($messages, message); messagePresenter.showInfo($messages, message);
}); });
}, }
registrationFailure: function(apiResponse) { function registrationFailure(apiResponse) {
messagePresenter.showError($messages, apiResponse.json && apiResponse.json.error || apiResponse); messagePresenter.showError($messages, apiResponse.json && apiResponse.json.error || apiResponse);
}, }
};
function render() {
$el.html(template());
$el.find('form').submit(eventHandlers.registrationFormSubmit);
$messages = $el.find('.messages');
$messages.width($el.find('form').width());
};
function validateRegistrationData(registrationData) { function validateRegistrationData(registrationData) {
if (registrationData.userName.length == 0) { if (registrationData.userName.length == 0) {

View file

@ -4,21 +4,18 @@ App.Presenters = App.Presenters || {};
App.Presenters.TopNavigationPresenter = function(util, jQuery, appState) { App.Presenters.TopNavigationPresenter = function(util, jQuery, appState) {
var selectedElement = null; var selectedElement = null;
var $el = jQuery('#top-navigation');
var template; var template;
util.loadTemplate('top-navigation').then(function(html) { util.loadTemplate('top-navigation').then(function(html) {
template = _.template(html); template = _.template(html);
render(); init();
}); });
var $el = jQuery('#top-navigation');
var eventHandlers = { function init() {
loginStateChanged: function() {
render(); render();
}, appState.startObserving('loggedIn', 'top-navigation', loginStateChanged);
}; }
appState.startObserving('loggedIn', 'top-navigation', eventHandlers.loginStateChanged);
function select(newSelectedElement) { function select(newSelectedElement) {
selectedElement = newSelectedElement; selectedElement = newSelectedElement;
@ -26,6 +23,10 @@ App.Presenters.TopNavigationPresenter = function(util, jQuery, appState) {
$el.find('li.' + selectedElement).addClass('active'); $el.find('li.' + selectedElement).addClass('active');
}; };
function loginStateChanged() {
render();
}
function render() { function render() {
$el.html(template({loggedIn: appState.get('loggedIn')})); $el.html(template({loggedIn: appState.get('loggedIn')}));
$el.find('li.' + selectedElement).addClass('active'); $el.find('li.' + selectedElement).addClass('active');

View file

@ -7,7 +7,11 @@ App.Presenters.UserListPresenter = function(jQuery, topNavigationPresenter, appS
var $el = jQuery('#content'); var $el = jQuery('#content');
init();
function init() {
render(); render();
}
function render() { function render() {
$el.html('Logged in: ' + appState.get('loggedIn')); $el.html('Logged in: ' + appState.get('loggedIn'));