I am trying, in vain it seems, to be able to pass additional parameters back to the success callback method that I have created for a successful ajax call. A little backgro
this is Out of ScopeJust so you (or anyone else) understand(s)1, the reason that this was undefined in loadImagesInSelect() in your original example was because that function was under a different scope, and scopes don't "cascade" like that2.
When you specified a "context" of selectBox in your AJAX call, it set the context (this) for the functions given to "success" and "error". (It just so happens that they were both anonymous functions.) At this point, this is defined in both of these functions, as the value of selectBox. Now, in the function passed to "success", you call loadImagesInSelect(). When you call a function, it creates a new scope. In this scope, this will be window in non-strict mode and undefined in strict mode.
// this = window (global scope)
$j = jQuery.noConflict();
function getImages(urlValue, selectBox) {
// this = undefined (function scope level 1)
$j.ajax({
type: "GET",
url: $j(urlValue).val(),
dataType: "jsonp",
context: selectBox,
success: function(data) {
// this = selectBox (function scope level 2)
loadImagesInSelect(data);
},
error: function(xhr, ajaxOptions, thrownError) {
// this = selectBox (function scope level 2)
alert(xhr.status);
alert(thrownError);
}
});
}
function loadImagesInSelect(data) {
// this = undefined (function scope level 3)
var select = $j(this);
select.empty();
$j(data).each(function() {
var theValue = $j(this)[0]["@value"];
var theId = $j(this)[0]["@name"];
select.append("");
});
select.children(":first").attr("selected", true);
}
$.proxy() is RedundantThe use of $.proxy() in your updated code is redundant. You use $.proxy() to get this to the function that was formerly called loadImagesInSelect(), but you moved that function up into "success" anyway (instead of calling it from within "success"). It already has access now to the value of this specified by "context".
You could remove the $.proxy() wrapper around your "success" function in your updated code and it would still work.
I know it's been years since you asked your question, but I hope this helps.
this variable) still isn't available in the inner function.undefined with window for non-strict mode.Function.prototype.bind() in ECMAScript 5.