Skip to content

Instantly share code, notes, and snippets.

@misternu
Created January 12, 2016 19:29
Show Gist options
  • Select an option

  • Save misternu/ab70474db59587c4c8a5 to your computer and use it in GitHub Desktop.

Select an option

Save misternu/ab70474db59587c4c8a5 to your computer and use it in GitHub Desktop.
$(document).ready(function() {
getNewHorsePage()
.done(makeHiddenForm);
$('#new-horse').on('click', function() {
event.preventDefault();
toggleForm();
});
$('body').on('submit', '#horse-form', function() {
event.preventDefault();
submitHorse(this)
.done(toggleForm)
.done(addToList)
.done(resetForm)
.fail(showErrors)
});
$('#horse-list').on('click', 'a', function(){
event.preventDefault();
showHorseInfo(this);
});
});
function getNewHorsePage() {
return $.ajax({
url: "/horses/new",
method: "GET"
});
}
function submitHorse(form) {
return $.ajax({
url: "/horses",
method: "POST",
data: $(form).serialize()
});
};
function makeHiddenForm(html) {
$('.container').append(html)
$('#new-horse-form-container').toggle();
}
function addToList(html) {
$('#horse-list').append(html);
}
function resetForm() {
$('.errors *').remove();
$('form').trigger('reset');
}
function showErrors(errors) {
$('.errors *').remove();
$('.errors').append(errors.responseText);
}
function showHorseInfo(horseLink) {
var info = $(horseLink).parent().siblings().toggle();
$('.horse-info').not(info).hide();
}
function toggleForm() {
$('#new-horse-form-container').toggle();
$('#new-horse').toggle();
}
@misternu
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment