Misunderstood how 'this' can change in JS

This commit is contained in:
Rick Falkvinge
2019-01-31 14:24:07 +01:00
parent 236709e3f4
commit d7dc48b3a8
2 changed files with 5 additions and 4 deletions
@@ -51,7 +51,6 @@
$(".LocalIconUndo").attr('src', '/Images/Icons/iconshock-balloon-undo-128x96px.png').hide();
$(".LocalIconWait").attr('src', '/Images/Abstract/ajaxloader-48x36px.gif').hide();
$(".LocalIconApproval").click(function () {
var itemId = $(this).attr("baseid");
$(this).hide();
@@ -61,6 +60,7 @@
SwarmopsJS.proxiedAjaxCall(
"/Pages/v5/Financial/ValidateReceipts.aspx/Validate",
{ identifier: itemId },
this,
function (result) {
if (result.Success) {
var itemId = $(this).attr("baseid");
@@ -91,6 +91,7 @@
SwarmopsJS.proxiedAjaxCall(
"/Pages/v5/Financial/ValidateReceipts.aspx/RetractValidation",
{ identifier: $(this).attr("baseid") },
this,
function(result) {
if (result.Success) {
var itemId = $(this).attr("baseid");
+3 -3
View File
@@ -450,12 +450,12 @@ var SwarmopsJS = (function () {
// Preserves the "this" object into success and error functions
publicSymbols.proxiedAjaxCall = proxiedAjaxCall;
function proxiedAjaxCall(url, params, successFunction, errorFunction) {
function proxiedAjaxCall(url, params, thisObject, successFunction, errorFunction) {
ajaxCall(
url,
params,
$.proxy(successFunction, this),
errorFunction !== undefined? $.proxy(errorFunction, this) : undefined
$.proxy(successFunction, thisObject),
errorFunction !== undefined? $.proxy(errorFunction, thisObject) : undefined
);
}