我在我的视图对象中将我的 self 方法引用为 this.removeThanksContainer 这通常很好,但是当我在我的 $.post() 回调中时,对的引用this
成为全局窗口对象而不是我的视图...... .
removeThanksContainer : function() {
var $thanksContainer = $('#js-review-thanks');
$thanksContainer.is(':visible') && $thanksContainer.remove();
},
getProductInfo : function(evt) {
// local method
var removeThanksContainer = this.removeThanksContainer;
$.post(postURL, { product_id : productId }, function(data) {
if (data) {
removeThanksContainer();
}
}, 'json');
}
因此,作为一种解决方法,我将对该方法的引用保存到 getProductInfo 中的局部变量,var removeThanksContainer = this.removeThanksContainer;
以便在 $.post() 中访问它。这是做到这一点的最佳方法还是有首选/更好的方法?
谢谢。