callback functions and type added to postChange function

This commit is contained in:
Claudio Barril 2014-01-29 22:17:25 -03:00
parent f0e056c298
commit 26138188ea

View File

@ -107,75 +107,115 @@
$('#changeInfoMsg').hide(); $('#changeInfoMsg').hide();
$('#languageMsg').hide(); $('#languageMsg').hide();
function postChange(url, postBody, msgContainerId, successMsg) { function postChange(url, postBody, onSuccess, onError, type) {
// Change success message ... // Change success message ...
jQuery.ajax(url, { jQuery.ajax(url, {
async: false, async: false,
dataType: 'json', dataType: 'json',
data: postBody, data: postBody,
type: 'PUT', type: type ? type : 'PUT',
contentType: "text/plain; charset=utf-8", contentType: "text/plain; charset=utf-8",
success: function (data, textStatus, jqXHR) { success: function (data, textStatus, jqXHR) {
$('#' + msgContainerId).removeClass('alert-error').addClass('alert-info').show(); onSuccess()
$('#' + msgContainerId).text(successMsg);
}, },
error: function (jqXHR, textStatus, errorThrown) { error: function (jqXHR, textStatus, errorThrown) {
$('#' + msgContainerId).removeClass('alert-info').addClass('alert-error').show(); onError(textStatus)
$('#' + msgContainerId).text(textStatus);
} }
}); });
} }
$('#changePasswordForm').submit(function (event) { $('#changePasswordForm').submit(function (event) {
var inputVal = $('#changePasswordForm #password').val(); var inputVal = $('#changePasswordForm #password').val();
var rinputVal = $('#changePasswordForm #repassword').val(); var rinputVal = $('#changePasswordForm #repassword').val();
var changePasswordMsg = $('#changePasswordMsg');
if (inputVal != rinputVal) { if (inputVal != rinputVal) {
// Password mismatch message ... // Password mismatch message ...
$('#changePasswordMsg').removeClass('alert-info').addClass('alert-error').show(); changePasswordMsg.removeClass('alert-info').addClass('alert-error').show();
$('#changePasswordMsg').text('<spring:message code="PASSWORD_MISSMATCH"/>'); changePasswordMsg.text('<spring:message code="PASSWORD_MISSMATCH"/>');
} else { } else {
postChange("c/restful/account/password", inputVal, 'changePasswordMsg', "<spring:message code="CHANGE_PASSWORD_SUCCESS"/>"); postChange(
"c/restful/account/password",
inputVal,
function() {
changePasswordMsg.removeClass('alert-error').addClass('alert-info').show();
changePasswordMsg.text('<spring:message code="CHANGE_PASSWORD_SUCCESS"/>');
},
function(textStatus) {
changePasswordMsg.removeClass('alert-info').addClass('alert-error').show();
changePasswordMsg.text(textStatus);
}
);
} }
event.preventDefault(); event.preventDefault();
}); });
$('#changeUserForm').submit(function (event) { $('#changeUserForm').submit(function (event) {
var firstname = $('#changeUserForm #firstname').val();
var fistname = $('#changeUserForm #firstname').val();
var lastname = $('#changeUserForm #lastname').val(); var lastname = $('#changeUserForm #lastname').val();
postChange("c/restful/account/firstname", fistname, 'changeInfoMsg', "<spring:message code="INFO_UPDATE_SUCCESS"/>"); var changeInfoMsg = $('#changeInfoMsg');
postChange("c/restful/account/lastname", lastname, 'changeInfoMsg', "<spring:message code="INFO_UPDATE_SUCCESS"/>"); postChange(
"c/restful/account/firstname",
firstname,
function() {
var changeInfoMsg = $('#changeInfoMsg');
changeInfoMsg.removeClass('alert-error').addClass('alert-info').show();
changeInfoMsg.text('<spring:message code="INFO_UPDATE_SUCCESS"/>');
},
function(textStatus) {
changeInfoMsg.removeClass('alert-info').addClass('alert-error').show();
changeInfoMsg.text(textStatus);
}
);
postChange(
"c/restful/account/lastname",
lastname,
function() {
changeInfoMsg.removeClass('alert-error').addClass('alert-info').show();
changeInfoMsg.text('<spring:message code="INFO_UPDATE_SUCCESS"/>');
},
function(textStatus) {
changeInfoMsg.removeClass('alert-info').addClass('alert-error').show();
changeInfoMsg.text(textStatus);
}
);
event.preventDefault(); event.preventDefault();
}); });
$('#languageForm').submit(function (event) { $('#languageForm').submit(function (event) {
var locale = $('#languageForm option:selected').val(); var locale = $('#languageForm option:selected').val();
postChange("c/restful/account/locale", locale, 'languageMsg', "<spring:message code="INFO_UPDATE_SUCCESS"/>"); var languageMsg = $('#languageMsg');
postChange(
"c/restful/account/locale",
locale,
function() {
languageMsg.removeClass('alert-error').addClass('alert-info').show();
languageMsg.text('<spring:message code="INFO_UPDATE_SUCCESS"/>');
},
function(textStatus) {
languageMsg.removeClass('alert-info').addClass('alert-error').show();
languageMsg.text(textStatus);
}
);
event.preventDefault(); event.preventDefault();
}); });
$('#deleteAccountForm').submit(function (event) { $('#deleteAccountForm').submit(function (event) {
var locale = $('#deleteAccountForm option:selected').val(); var locale = $('#deleteAccountForm option:selected').val();
//FIXME: este metodo hay que unificarlo con postChange, con callbacks o eventos para los success and error y pasandole el type postChange(
jQuery.ajax("c/restful/account", { "c/restful/account",
async: false, locale,
dataType: 'json', function() {
data: locale,
type: 'DELETE',
contentType: "text/plain; charset=utf-8",
success: function (data, textStatus, jqXHR) {
window.location.href = "/c/logout" window.location.href = "/c/logout"
}, },
error: function (jqXHR, textStatus, errorThrown) { function(textStatus) {
$('#' + 'deleteAccountMsg').removeClass('alert-info').addClass('alert-error').show(); var deleteAccountMsg = $('#deleteAccountMsg');
$('#' + 'deleteAccountMsg').text(textStatus); deleteAccountMsg.removeClass('alert-info').addClass('alert-error').show();
} deleteAccountMsg.text(textStatus);
}); },
'DELETE'
)
event.preventDefault(); event.preventDefault();
}); });
</script> </script>