I’m referencing my self method(s) within my view object as this.removeThanksContainer which is fine normally,, but when I am inside my $.post() callback, the reference to this becomes the global window object and not my view….
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');
}
So as a workaround I’m saving a reference to that method to a local variable within getProductInfo as var removeThanksContainer = this.removeThanksContainer; to access it within $.post(). Is this the best way to do this or is there a preferred/better method?
Thanks.
You can also keep a reference to a
thislikeand call
that.removeThanksContainer();in the$.postcallback.