I have a CoffeeScript object which is giving me a strange error after an action is triggered.
The object loads without incident, although once the action is completed that triggers the callback I receive the error:
this.update is not a function return this.update(value);
Does anyone have an idea why this error has occurred? My best guess is the this object inside the jQuery.rating call is actually referring to a jQuery object, rather than the rating object?
My CoffeeScript code is:
jQuery ->
new Rating()
class Rating
constructor: ->
$('.auto-submit-star').rating
callback:
(value, link) -> @update value
update: (value) =>
$.ajax
type: 'post'
url: $('#new_rating').attr('action')
data: 'rating': value
.done ( msg ) ->
alert( msg )
The code compiles to:
var Rating,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Rating = (function() {
function Rating() {
this.update = __bind(this.update, this);
$('.auto-submit-star').rating({
callback: function(value, link) {
return this.update(value);
}
});
}
Rating.prototype.update = function(value) {
return $.ajax({
type: 'post',
url: $('#new_rating').attr('action'),
data: {
'rating': value
}
}).done(function(msg) {
return alert(msg);
});
};
return Rating;
})();