Skip to content

Instantly share code, notes, and snippets.

@kirillsulim
Last active August 29, 2015 14:24
Show Gist options
  • Save kirillsulim/f8e333a829b07787bccd to your computer and use it in GitHub Desktop.
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 =)
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));
});
});
$(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");
});
}
);
});
});
});
@kirillsulim
Copy link
Author

.bind is crucial for getting rid of callback hell =)

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