【发布时间】:2015-06-01 00:16:53
【问题描述】:
我有一个 CoffeeScript 对象,它在触发操作后给我一个奇怪的错误。
虽然触发回调的操作完成后,我收到错误消息:
this.update 不是函数 返回 this.update(value);
有人知道为什么会发生这个错误吗?我最好的猜测是 jQuery.rating 调用中的 this 对象实际上是指一个 jQuery 对象,而不是 rating 对象?
我的 CoffeeScript 代码是:
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 )
代码编译为:
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;
})();
【问题讨论】:
标签: jquery ruby-on-rails coffeescript