I currntly have a nameless function in a javascript ajax-call:
(This code has been cut down to the essentials, there might be some errors)
function call(name){
var type = services[name].type
$.oajax({
success: function(data) {
fbposts=data.data
if (type === "grupp"){
var postid=fbposts[fbpost].id.split("_");
return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/'
}
else if (fbposts[fbpost].actions){
return fbposts[fbpost].actions[0].link;
}
}
}
})
};
I want to insead use
success: successfunction,
and the reference the function like this:
function success(data) {
fbposts=data.data
if (type === "grupp"){
var postid=fbposts[fbpost].id.split("_");
return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/'
}
else if (fbposts[fbpost].actions){
return fbposts[fbpost].actions[0].link;
}
}
}
})
At this point, the type-variable is no longer defined. Can I somehow solve this?
One approach that could work is to make use of the
contextparameter on$.ajax— for example:Using
contextcausesthisinside yoursuccessfunction to point to whatever you passed in forcontext. So in the above example, when withinsuccessFunctionwe readthis would mean that
typerefers tothis.type, which is the same value oftypethat we saved intocontextfrom withincall.