PDA

View Full Version : Alert component and scope


hobbis
07-05-2005, 10:36 AM
Here's an interesting problem that had me stumped for a while. Using the Alert component handler function caused a scope issue that prevented me form passing any variables to the handler function.


var a = Alert.show("Are you sure you want to delete the file?", "Delete File", Alert.YES | Alert.NO, _parent, onDeleteDownloadPrompt, "warning2_mc", Alert.NO)
a.move(Stage.width/2 - a.width/2, Stage.height/2 - a.height/2);


private function onDeleteDownloadPrompt(eventObj:Object):Boolean {
trace(this); //actually refers to the Alert box instance
}


The problem with the above code is that if I wanted to pass an event object to the Alert box click handler I couldn't. I tried creating a class scope var, copying the object to that and then using it in the click handler. Scope problems!!

The answer is to pass any objects or information through Alert box instance properties like so:


var a = Alert.show("Are you sure you want to delete " + filename + " ?", "Delete Download File", Alert.YES | Alert.NO, eventObj.thisParent, onDeleteDownloadPrompt, "warning2_mc", Alert.NO)
a.move(Stage.width/2 - a.width/2, Stage.height/2 - a.height/2);
a.tempObject = eventObj;


private function onDeleteDownloadPrompt(eventObj:Object):Boolean {
trace("---------------------- ::: " + eventObj.target.tempObject)
for(var i in eventObj.target.tempObject) {
trace(i + " ===== " + eventObj.target.tempObject[i])
}
}