Skip to content

Instantly share code, notes, and snippets.

@matagus
Created September 23, 2014 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matagus/8d9debbaaa451eb19ea5 to your computer and use it in GitHub Desktop.
Save matagus/8d9debbaaa451eb19ea5 to your computer and use it in GitHub Desktop.
favorite.js for django-favit with support for dynamic items added at any time
$(document).ready(function() {
$(".endless_container").on("click", ".btn.favorite", function() {
var $obj = $(this);
var target_id = $obj.attr('id').split('_')[1];
$obj.prop('disabled', true);
$.ajax({
url: $obj.attr('href'),
type: 'POST',
data: {target_model: $obj.attr('model'),
target_object_id: target_id},
success: function(response) {
if (response.status == 'added') {
$obj.children().removeClass('icon-heart-empty').addClass('icon-heart');}
else {
$obj.children().removeClass('icon-heart').addClass('icon-heart-empty');
}
$obj.parent('.favit').children('.fav-count').text(response.fav_count);
$obj.prop('disabled', false);
}
});
});
$(".endless_container").on("click", ".btn.unfave", function() {
var $obj = $(this);
$obj.prop('disabled', true);
$.ajax({
url: $obj.attr('href'),
type: 'POST',
data: {
target_model: $obj.data('model'),
target_object_id: $obj.data('id')
},
success: function(response) {
if (response.status == 'deleted') {
$obj.parent().remove();
}
},
complete: function(response) {
$obj.prop('disabled', false);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment