Skip to content

Instantly share code, notes, and snippets.

@galileoguzman
Created August 20, 2015 20:53
Show Gist options
  • Save galileoguzman/9a09cc3e78a0767b5341 to your computer and use it in GitHub Desktop.
Save galileoguzman/9a09cc3e78a0767b5341 to your computer and use it in GitHub Desktop.
Client of Ticket System API
/*
* New Ticket Jquery
* Is a file for get content of div#new-ticket
* And send to backend with laravel.
**/
$(document).on('ready', begin);
function begin()
{
//this method initialize functions when document is loaded.
$('#publish').on('click', function(e){
//this method will execute ajax method for send data to backend.
e.preventDefault();
// check status of request
if (sendTicketToBackend(buildTicket()) ){
// Catch bind of close modal
$('#modal-message').on('hidden.bs.modal', function () {
// Redirect to index view
window.location.replace("http://localhost:8888/mayestro/public/");
});
}else{
console.log("error");
}
});
}
function buildTicket()
{
/**
* This method builds the model of article to
* be send to the backend, return JSON object.
**/
var ticket = {
'title' : $('#ticket_title').val(),
'description' : $('#ticket_descripction').val(),
'category' : $('#ticket_category').val(),
'_token' : $('#_token').val()
};
return ticket;
}
function sendTicketToBackend(ticket)
{
/**
* This method recived a object JSON to be'll send
* to the backend by AJAX (POST request).
**/
$.ajax({
type : 'POST',
url : 'http://localhost:8888/mayestro/public/tickets',
data : ticket,
dataType: 'JSON',
cache : false,
beforeSend : function(){
//Before to send request do a animation
// ...
},
error: function(request, status, error){
// If there is a error do something like show message
console.log(request.responseText);
return false;
},
success: function(response){
// If backend had to send you a response do something
// ...
$('.modal-body p').text(response.message);
if(response.response_code == 200){
$('#thumb-msg').addClass('btn-success');
}else if(response.response_code == 201){
$('#thumb-msg').addClass('btn-danger');
}else{
$('#thumb-msg').addClass('btn-warning');
}
$('#modal-message').modal('show');
return true;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment