Restructured presenters code
This commit is contained in:
parent
aed090da7d
commit
7c8e473e1b
5 changed files with 98 additions and 91 deletions
|
@ -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,
|
||||
};
|
||||
|
|
|
@ -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. <a href="">Back to main page</a>');
|
||||
$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. <a href="">Back to main page</a>');
|
||||
$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 {};
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -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 += '<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 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 += '<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) {
|
||||
if (registrationData.userName.length == 0) {
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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'));
|
||||
|
|
Loading…
Reference in a new issue