Created
March 31, 2016 22:19
-
-
Save jkarnowski/878a6da5373d46c2fbf0c01d6e52639a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
bindListeners(); | |
}); | |
var bindListeners = function(){ | |
getHorseFormListener(); | |
postNewHorseFormListener(); | |
} | |
function getHorseFormListener(){ | |
$('#new-horse').on('click', requestForm); | |
} | |
function postNewHorseFormListener(){ | |
$('#horse-list').on("submit", "#horse-form", function(event){ | |
event.preventDefault(); | |
var formData = $(this).serialize(); | |
console.log("submitting: " + formData); | |
submitForm(formData); | |
}) | |
} | |
var requestForm = function(event){ | |
event.preventDefault(); | |
$('#new-horse').hide(); | |
var url = $(this).attr('href'); | |
$.ajax({ | |
url: url, | |
type: 'GET' | |
}) | |
.done(function(response){ | |
console.log("response: " + response); | |
$('#horse-list').append(response); | |
}) | |
.fail(function(xhr, status, errorMessage){ | |
// what to see on a failing request | |
console.log("failing the request!") | |
console.log("status code: " + status); | |
console.log("errorMessage: " + errorMessage); | |
console.dir(xhr); | |
}) | |
} | |
var submitForm = function(formData){ | |
$.ajax({ | |
url: '/horses', | |
type: 'POST', | |
data: formData | |
}) | |
.done(function(response){ | |
// do something here | |
console.log("SUCCESS " + response); | |
$('#horse-list').prepend(response); | |
}) | |
.fail(function(status, errorMessage){ | |
console.log("Failing!!!") | |
console.log("status: " + status) | |
console.log("errorMessage: " + errorMessage) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment