Last active
August 29, 2015 14:24
-
-
Save kirillsulim/f8e333a829b07787bccd to your computer and use it in GitHub Desktop.
Welcome to callback hell! This is a little slice of callback hell =) Enjoy! I will rewrite it =)
This file contains hidden or 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
function notifyServerError() { | |
swal("Server failed"); | |
}; | |
function hidePost(id) { | |
$('div[data-post-id=' + id + ']').hide("slow"); | |
}; | |
var update_url = '/news/update_state'; | |
var STATE_PUBLISHED = 'P'; | |
var STATE_DELETED = 'X'; | |
function handlePublish(id) { | |
swal( | |
{ | |
title: 'Вы уверены?', | |
type: 'warning', | |
showCancelButton: true, | |
cancelButtonText: 'Отмена', | |
confirmButtonClass: 'btn-success', | |
confirmButtonText: 'Опубликовать' | |
}, | |
function(){ | |
$.post(update_url, { | |
id: id, | |
state: STATE_PUBLISHED | |
}) | |
.done(hidePost.bind(this, id)) | |
.fail(notifyServerError); | |
} | |
); | |
}; | |
function handleDelete(id) { | |
swal( | |
{ | |
title: 'Вы уверены?', | |
type: 'warning', | |
showCancelButton: true, | |
cancelButtonText: 'Отмена', | |
confirmButtonClass: 'btn-danger', | |
confirmButtonText: 'Удалить' | |
}, | |
function(){ | |
$.post(update_url, { | |
id: id, | |
state: STATE_DELETED | |
}) | |
.done(hidePost.bind(this, id)) | |
.fail(notifyServerError); | |
} | |
); | |
}; | |
$(function(){ | |
$('.btn-publish').each(function(index){ | |
var id = $(this).data('post-id'); | |
$(this).click(handlePublish.bind(this, id)); | |
}); | |
$('.btn-delete').each(function(index){ | |
var id = $(this).data('post-id'); | |
$(this).click(handleDelete.bind(this, id)); | |
}); | |
}); |
This file contains hidden or 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
$(function(){ | |
$('.btn-publish').each(function(index){ | |
var id = $(this).data('post-id'); | |
$(this).click(function(){ | |
swal( | |
{ | |
title: 'Вы уверены?', | |
type: 'warning', | |
showCancelButton: true, | |
cancelButtonText: 'Отмена', | |
confirmButtonClass: 'btn-success', | |
confirmButtonText: 'Опубликовать' | |
}, | |
function(){ | |
$.post('/news/update_state', { | |
id: id, | |
state: 'P' | |
}) | |
.done(function(){ | |
$('div[data-post-id=' + id + ']').hide("slow"); | |
}) | |
.fail(function(){ | |
swal("Server failed"); | |
}); | |
} | |
); | |
}); | |
}); | |
$('.btn-delete').each(function(index){ | |
var id = $(this).data('post-id'); | |
$(this).click(function(){ | |
swal( | |
{ | |
title: 'Вы уверены?', | |
type: 'warning', | |
showCancelButton: true, | |
cancelButtonText: 'Отмена', | |
confirmButtonClass: 'btn-danger', | |
confirmButtonText: 'Удалить' | |
}, | |
function(){ | |
$.post('/news/update_state', { | |
id: id, | |
state: 'X' | |
}) | |
.done(function(){ | |
$('div[data-post-id=' + id + ']').hide("slow"); | |
}) | |
.fail(function(){ | |
swal("Server failed"); | |
}); | |
} | |
); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.bind is crucial for getting rid of callback hell =)