Today i want to implement an easy contact form in one of my older projects but there was a strange behaviour.
I have created 2 new instances of LoadVars class and set a release event on to a button for calling a sendAndLoad event.
[as]
public function init():Void {
mailInfo = new LoadVars();
mailStatus = new LoadVars();
mailStatus._container = this;
mailStatus._container.sucField = _container.sucField;
mailStatus.onLoad = onMailLoad;
}
public function release():Void {
mailInfo.fVorname = _container.forename.text;
mailInfo.fNachname = _container.surename.text;
mailInfo.fEMail = _container.mailadress.text;
mailInfo.fFeedback = _container.feedback.text;
mailInfo.sendAndLoad(“kontakt.php”, mailStatus);
}
[/as]
This is the callback function
[as]
public function onMailLoad(success):Void {
if(success) {
_container.sucField._visible = true;
_container.sucField.gotoAndStop(1);
} else {
_container.sucField._visible = true;
_container.sucField.gotoAndStop(2);
}
}
[/as]
The callback is defined in the same scope where loadVars are defined.
The problem:
After compiling fla-file and testing contact form, i recieved email and get answer from server(i tested with packet-sniffer Etherreal), so i thougt, what the hell is wrong.
The next try i set an additional debug textfield in my fla and trace the reference to the class where the callback is defined and to the output field which is set visible in callback function.
Both where undefined and so thought about how it was in Action Script 1.0 . There you have to create an anonymous function if you wanna get a callback, or setting an other function to overwrite the LoadVars onLoad.
Example:
[as]
var l = new LoadVars();
var l2 = new LoadVars();
l2.onLoad = myFunction;
l.sendAndLoad(“test.php”, l2);
function myFunction() {
trace(“callback”);
}
[/as]
With this construct the scope of the function was to the LoadVars object l2.
In ActionScript 2.0 there is the same behaviour, the function onMailLoad will be called, but with another scope. It is not longer member of the class where the method is defined, but the method is member of loadVars instance.
very strange