cmc-sales/webroot/js/add_edit_user_ajax.js

76 lines
1.7 KiB
JavaScript
Raw Normal View History

2011-03-09 23:18:26 -08:00
$(function() {
/**
* Javascript to do the actual validation for Adding/Editing users in the modal window.
*/
2011-03-09 23:18:26 -08:00
var _loadingDiv = $("#ajaxLoading");
$('#submitUserButton').button();
$('#UserAddUserForm').submit(function(){
2011-03-09 23:18:26 -08:00
_loadingDiv.show();
$.post('/users/add_user',
$(this).serializeArray(),
afterValidate,
"json"
);
return false;
});
// Post-submit callback
function afterValidate(data, status) {
$(".message").remove();
$(".error-message").remove();
if (data.errors) {
onError(data.errors);
} else if (data.success) {
onSuccess(data.success);
}
}
function onSuccess(data) {
flashMessage(data.message);
_loadingDiv.hide();
$('#submitUserButton').button('disable');
2011-03-09 23:18:26 -08:00
window.setTimeout(function() {
// window.location.href = '/posts';
window.location.reload(true);
2011-03-10 17:52:33 -08:00
}, 500);
2011-03-09 23:18:26 -08:00
};
function onError(data) {
flashMessage(data.message);
$.each(data.data, function(model, errors) {
for (fieldName in this) {
var element = $("#" + camelize(model + '_' + fieldName));
var _insert = $(document.createElement('div')).insertAfter(element);
_insert.addClass('error-message').text(this[fieldName])
}
_loadingDiv.hide();
});
};
function flashMessage(message) {
var _insert = $(document.createElement('div')).css('display', 'none');
_insert.attr('id', 'flashMessage').addClass('message').text(message);
_insert.insertAfter($("#UserAddUserForm")).fadeIn();
2011-03-09 23:18:26 -08:00
}
function camelize(string) {
var a = string.split('_'), i;
s = [];
for (i=0; i<a.length; i++){
s.push(a[i].charAt(0).toUpperCase() + a[i].substring(1));
}
s = s.join('');
return s;
}
});