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,42 +18,42 @@ 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'))
loginFormSubmit: function(e) { router.navigateToMainPage();
e.preventDefault(); else
messagePresenter.hideMessages($messages); render();
}
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 render() { function render() {
$el.html(template()); $el.html(template());
$el.find('form').submit(eventHandlers.loginFormSubmit); $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());
}; };
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 { 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();
},
};
auth.logout().then(function() { function init() {
var $messageDiv = messagePresenter.showInfo($messages, 'Logged out. <a href="">Back to main page</a>'); auth.logout().then(function() {
$messageDiv.find('a').click(eventHandlers.mainPageLinkClick); var $messageDiv = messagePresenter.showInfo($messages, 'Logged out. <a href="">Back to main page</a>');
}).catch(function(response) { $messageDiv.find('a').click(mainPageLinkClicked);
messagePresenter.showError($messages, response.json && response.json.error || response); }).catch(function(response) {
}); messagePresenter.showError($messages, response.json && response.json.error || response);
});
}
return { function mainPageLinkClicked(e) {
}; e.preventDefault();
router.navigateToMainPage();
}
return {};
}; };

View file

@ -15,53 +15,54 @@ 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) { }
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 += '<a href="#/login">Click here</a> to login.';
messagePresenter.showInfo($messages, message);
});
},
registrationFailure: function(apiResponse) {
messagePresenter.showError($messages, apiResponse.json && apiResponse.json.error || apiResponse);
},
};
function render() { function render() {
$el.html(template()); $el.html(template());
$el.find('form').submit(eventHandlers.registrationFormSubmit); $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());
}; }
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 += '<a href="#/login">Click here</a> to login.';
messagePresenter.showInfo($messages, message);
});
}
function registrationFailure(apiResponse) {
messagePresenter.showError($messages, apiResponse.json && apiResponse.json.error || apiResponse);
}
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');
render(); init();
function init() {
render();
}
function render() { function render() {
$el.html('Logged in: ' + appState.get('loggedIn')); $el.html('Logged in: ' + appState.get('loggedIn'));