From 7c8e473e1b1af9cb630e17b0eeed94c33e25f676 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 2 Sep 2014 23:07:50 +0200 Subject: [PATCH] Restructured presenters code --- public_html/js/Presenters/LoginPresenter.js | 54 ++++++------- public_html/js/Presenters/LogoutPresenter.js | 29 +++---- .../js/Presenters/RegistrationPresenter.js | 81 ++++++++++--------- .../js/Presenters/TopNavigationPresenter.js | 19 ++--- .../js/Presenters/UserListPresenter.js | 6 +- 5 files changed, 98 insertions(+), 91 deletions(-) diff --git a/public_html/js/Presenters/LoginPresenter.js b/public_html/js/Presenters/LoginPresenter.js index 34ae0c08..3b8ac20e 100644 --- a/public_html/js/Presenters/LoginPresenter.js +++ b/public_html/js/Presenters/LoginPresenter.js @@ -18,42 +18,42 @@ App.Presenters.LoginPresenter = function( util.loadTemplate('login-form').then(function(html) { template = _.template(html); - render(); + init(); }); - var eventHandlers = { - - loginFormSubmit: function(e) { - e.preventDefault(); - messagePresenter.hideMessages($messages); - - var userName = $el.find('[name=user]').val(); - var password = $el.find('[name=password]').val(); - var remember = $el.find('[name=remember]').val(); - - //todo: client side error reporting - - auth.loginFromCredentials(userName, password, remember) - .then(function(response) { - router.navigateToMainPage(); - //todo: "redirect" to main page - }).catch(function(response) { - messagePresenter.showError($messages, response.json && response.json.error || response); - }); - }, - - }; - - if (appState.get('loggedIn')) - router.navigateToMainPage(); + function init() { + if (appState.get('loggedIn')) + router.navigateToMainPage(); + else + render(); + } function render() { $el.html(template()); - $el.find('form').submit(eventHandlers.loginFormSubmit); + $el.find('form').submit(loginFormSubmitted); $messages = $el.find('.messages'); $messages.width($el.find('form').width()); }; + function loginFormSubmitted(e) { + e.preventDefault(); + messagePresenter.hideMessages($messages); + + var userName = $el.find('[name=user]').val(); + var password = $el.find('[name=password]').val(); + var remember = $el.find('[name=remember]').val(); + + //todo: client side error reporting + + auth.loginFromCredentials(userName, password, remember) + .then(function(response) { + router.navigateToMainPage(); + //todo: "redirect" to main page + }).catch(function(response) { + messagePresenter.showError($messages, response.json && response.json.error || response); + }); + } + return { render: render, }; diff --git a/public_html/js/Presenters/LogoutPresenter.js b/public_html/js/Presenters/LogoutPresenter.js index ac0b6e35..882b2271 100644 --- a/public_html/js/Presenters/LogoutPresenter.js +++ b/public_html/js/Presenters/LogoutPresenter.js @@ -12,22 +12,23 @@ App.Presenters.LogoutPresenter = function( var $messages = jQuery('#content'); - var eventHandlers = { - mainPageLinkClick: function(e) { - e.preventDefault(); - router.navigateToMainPage(); - }, - }; + init(); - auth.logout().then(function() { - var $messageDiv = messagePresenter.showInfo($messages, 'Logged out. Back to main page'); - $messageDiv.find('a').click(eventHandlers.mainPageLinkClick); - }).catch(function(response) { - messagePresenter.showError($messages, response.json && response.json.error || response); - }); + function init() { + auth.logout().then(function() { + var $messageDiv = messagePresenter.showInfo($messages, 'Logged out. Back to main page'); + $messageDiv.find('a').click(mainPageLinkClicked); + }).catch(function(response) { + messagePresenter.showError($messages, response.json && response.json.error || response); + }); + } - return { - }; + function mainPageLinkClicked(e) { + e.preventDefault(); + router.navigateToMainPage(); + } + + return {}; }; diff --git a/public_html/js/Presenters/RegistrationPresenter.js b/public_html/js/Presenters/RegistrationPresenter.js index 1b2912f2..e83f5866 100644 --- a/public_html/js/Presenters/RegistrationPresenter.js +++ b/public_html/js/Presenters/RegistrationPresenter.js @@ -15,53 +15,54 @@ App.Presenters.RegistrationPresenter = function( util.loadTemplate('registration-form').then(function(html) { template = _.template(html); - render(); + init(); }); - var eventHandlers = { - - registrationFormSubmit: function(e) { - e.preventDefault(); - messagePresenter.hideMessages($messages); - - registrationData = { - userName: $el.find('[name=user]').val(), - password: $el.find('[name=password1]').val(), - passwordConfirmation: $el.find('[name=password2]').val(), - email: $el.find('[name=email]').val(), - }; - - if (!validateRegistrationData(registrationData)) - return; - - api.post('/users', registrationData) - .then(function(response) { - eventHandlers.registrationSuccess(response); - }).catch(function(response) { - eventHandlers.registrationFailure(response); - }); - }, - - registrationSuccess: function(apiResponse) { - //todo: tell user if it turned out that he needs to confirm his e-mail - $el.find('form').slideUp(function() { - var message = 'Registration complete! '; - message += 'Click here to login.'; - messagePresenter.showInfo($messages, message); - }); - }, - - registrationFailure: function(apiResponse) { - messagePresenter.showError($messages, apiResponse.json && apiResponse.json.error || apiResponse); - }, - }; + function init() { + render(); + } function render() { $el.html(template()); - $el.find('form').submit(eventHandlers.registrationFormSubmit); + $el.find('form').submit(registrationFormSubmitted); $messages = $el.find('.messages'); $messages.width($el.find('form').width()); - }; + } + + function registrationFormSubmitted(e) { + e.preventDefault(); + messagePresenter.hideMessages($messages); + + registrationData = { + userName: $el.find('[name=user]').val(), + password: $el.find('[name=password1]').val(), + passwordConfirmation: $el.find('[name=password2]').val(), + email: $el.find('[name=email]').val(), + }; + + if (!validateRegistrationData(registrationData)) + return; + + api.post('/users', registrationData) + .then(function(response) { + registrationSuccess(response); + }).catch(function(response) { + registrationFailure(response); + }); + } + + function registrationSuccess(apiResponse) { + //todo: tell user if it turned out that he needs to confirm his e-mail + $el.find('form').slideUp(function() { + var message = 'Registration complete! '; + message += 'Click here to login.'; + messagePresenter.showInfo($messages, message); + }); + } + + function registrationFailure(apiResponse) { + messagePresenter.showError($messages, apiResponse.json && apiResponse.json.error || apiResponse); + } function validateRegistrationData(registrationData) { if (registrationData.userName.length == 0) { diff --git a/public_html/js/Presenters/TopNavigationPresenter.js b/public_html/js/Presenters/TopNavigationPresenter.js index 781a8103..ad658866 100644 --- a/public_html/js/Presenters/TopNavigationPresenter.js +++ b/public_html/js/Presenters/TopNavigationPresenter.js @@ -4,21 +4,18 @@ App.Presenters = App.Presenters || {}; App.Presenters.TopNavigationPresenter = function(util, jQuery, appState) { var selectedElement = null; + var $el = jQuery('#top-navigation'); var template; util.loadTemplate('top-navigation').then(function(html) { template = _.template(html); - render(); + init(); }); - var $el = jQuery('#top-navigation'); - var eventHandlers = { - loginStateChanged: function() { - render(); - }, - }; - - appState.startObserving('loggedIn', 'top-navigation', eventHandlers.loginStateChanged); + function init() { + render(); + appState.startObserving('loggedIn', 'top-navigation', loginStateChanged); + } function select(newSelectedElement) { selectedElement = newSelectedElement; @@ -26,6 +23,10 @@ App.Presenters.TopNavigationPresenter = function(util, jQuery, appState) { $el.find('li.' + selectedElement).addClass('active'); }; + function loginStateChanged() { + render(); + } + function render() { $el.html(template({loggedIn: appState.get('loggedIn')})); $el.find('li.' + selectedElement).addClass('active'); diff --git a/public_html/js/Presenters/UserListPresenter.js b/public_html/js/Presenters/UserListPresenter.js index c8794742..f7a46112 100644 --- a/public_html/js/Presenters/UserListPresenter.js +++ b/public_html/js/Presenters/UserListPresenter.js @@ -7,7 +7,11 @@ App.Presenters.UserListPresenter = function(jQuery, topNavigationPresenter, appS var $el = jQuery('#content'); - render(); + init(); + + function init() { + render(); + } function render() { $el.html('Logged in: ' + appState.get('loggedIn'));