Given the following code:
$.get("somepage.html", function (data) {
$("#someID").html(data);
});
data
goes out of scope immediately. It goes so far out of scope that it nukes any references to it so that
function jqAjaxGet(view){
$.get(view, function (data) {
return data;
});
}
returns undefined
and
var globalRetVal = "TestVal";
function jqAjaxGet(view){
$.get(view, function (data) {
globalRetVal = data;
});
return globalRetVal;
}
returns "TestVal
". I've run into this in the past and it's always been a matter of me abusing some other way of doing what I should have been doing but this time, I just plainly want to get the contents of a file and plop it into a container (similar to rendering a partial). I've found that a solution to this is to pass in a target ID so that I dump data
into that container immediately but I'd like to simplify the method signature because, hey, someday I might just want the contents of that thing for calculation instead of immediately displaying it.
What do I need to do to keep a reference or, better yet, return the value in data
because storing it in a global isn't working?