Skip to content

Instantly share code, notes, and snippets.

@drocarmo
Created December 18, 2013 21:53
Show Gist options
  • Save drocarmo/8030500 to your computer and use it in GitHub Desktop.
Save drocarmo/8030500 to your computer and use it in GitHub Desktop.
Sortable ajax JS
// Sortable shit
$(".connected").sortable({
connectWith: ".connected",
items: "> li",
placeholder: "sortable-placeholder",
revert: 100,
tolerance: "pointer",
scroll: false,
cancel: "[contenteditable], textarea, input",
// when all sorting starts
start: function() {
$(".trash").addClass("active");
},
// when all sorting ends
stop: function() {
$(".trash").removeClass("active");
},
// when user stops sorting and the DOM position has changed,
// currently buggy as it fires before and after delete happens
update: function() {
var dataIDList = $('.link-list li').map(function(){
return $(this).data("id");
}).get();
$.post("/w/sort",{ ids: dataIDList });
},
over: function() {
$(".trash").toggleClass("red");
}
});
// when conneceted sortable list has received an item from another list
// Delete posts when the enter the trash
$(".trash").bind("sortreceive", function(e, ui){
if ($(this).hasClass("red")) { $(this).removeClass("red") }
$.ajax({
url: '/links/' + $(".trash .link").data("id"),
type: 'POST',
dataType: "json",
data: {"_method":"delete"},
complete: function() {
$(".trash .link").remove();
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment