/
jquery.phileo.js
39 lines (33 loc) · 1.1 KB
/
jquery.phileo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
jQuery(function($) {
var PhileoLikes = function(form, options) {
this.options = $.extend({}, $.fn.phileo.defaults, options);
this.$form = $(form);
this.$count = $(this.options.count);
var self = this;
this.$form.submit(function(event) {
event.preventDefault();
$.ajax({
url: self.$form.attr('action'),
type: "POST",
data: self.$form.serialize(),
statusCode: {
200: function(data, textStatus, jqXHR) {
self.$count.text(data.likes_count);
self.$form[data.liked ? 'addClass' : 'removeClass'](self.options.toggle_class);
}
}
});
});
};
$.fn.phileo = function(options) {
$(this).each(function(i, el) {
var phileo = new PhileoLikes(el, options);
$(el).data('Phileo', {instance: phileo});
});
return this;
};
$.fn.phileo.defaults = {
toggle_class: 'phileo-liked',
count: false
};
});