It's possible to do what you want, but I wouldn't recommend it. Here's how:
function doTheThing() {
var element, to_list;
/* code here that gets element and to_list */
// Modify the post to use a callback and the "text" data type
$.post($(F).attr('action'), $(F).serialize(), handleMove, "text");
// Here's the callback
function handleMove(data) {
eval(data);
}
}
This works because eval
is very special and it executes in the scope in which it's called (it's a bit magic in that way), and so the code in the text eval
evaluates has access to all of the variables in scope where eval
is called.
Going slightly off-topic, but I would recommend a data-based approach instead. Perhaps:
function doTheThing() {
var element, to_list;
/* code here that gets element and to_list */
// Modify the post to use a callback and the "json" data type
$.post($(F).attr('action'), $(F).serialize(), handleMove, "json");
// Here's the callback
function handleMove(data) {
if (data.errorMessage) {
/* ...show the error... */
}
else {
moveNode(element, to_list);
}
}
}
...where your server either returns:
{"errorMessage": "Don't move it!"}
or
{"success": true}
or whatever makes sense in your environment. Heck, if it's really just "it worked" or "here's an error", you could just use a text protocol where "OK" meant okay and anything else was an error message (the internet is full of text-based protocols like that). I prefer more structure, but the point is you have options.
I would find that approach easier to maintain. When you start passing code back and forth between layers, code relying on knowing the names of in-scope variables, it seems like a major close-coupling issue.