Skip to content

Instantly share code, notes, and snippets.

@matagus

matagus/favorite.js

Created Sep 23, 2014
Embed
What would you like to do?
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