Model class
package flashscriptMVC
{
import flashscriptMVC.IModel;
import flash.events.*;
import flash.display.DisplayObject;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.navigateToURL;
import biz.Flashscript.utils.LoadData;
public class Model extends EventDispatcher implements IModel
{
protected var xmlData:XML;
private var alertText:String;
private var yesno:Boolean;
private var target:Object;
public static const ALERT_WINDOW:String = "alertWindow";
public static const VIEW_WINDOW:String = "viewWindow";
public static const SERVER_ERROR:String = "serverError";
public static const SERVER_RESPONSE:String = "serverResponse";
public function Model (myXML:String):void
{
/*
* The Model class is designed in that way that it can be used
* for other forms as well or for applications, which send data
* to the server and get a response from the server. It is completely
* separated from the viewers.
*
* A major task of the Model class is to load an XML file with
* data for the Login and Alert viewers. Those are Labels, URLs
* and messages. We could also add parameters for the movie here.
*/
var urlLoader:URLLoader = new URLLoader ;
var urlRequest = new URLRequest(myXML);
urlLoader.load (urlRequest);
urlLoader.addEventListener (Event.COMPLETE,onComplete);
}
public function onComplete (evt:Event):void
{
try
{
var xml:XML = new XML(evt.currentTarget.data);
setXML (xml);
}
catch (evt:TypeError)
{
trace ("Could not parse text into XML");
trace (evt.message);
}
}
/*
* The Model class has a number of getter - setter functions. One of
* the functions is to set focus on an object.
*/
public function focusObject (target:Object):void
{
target.setFocus ();
}
/*
* Functions to make the XML available for the viewers.
*/
public function setXML (_xml:XML):void
{
xmlData = _xml;
update ("viewWindow");
}
public function getXMLData ():XML
{
return xmlData;
}
/*
* Functions to set an Alert. The following functions set the
* Alert parameters and are Getters for the viewers to get the
* parameters.
*/
public function alert (alText:String,yn:Boolean,_target:Object):void
{
alertText = alText;
yesno = yn;
target = _target;
update ("alertWindow");
}
public function getAlertText ():String
{
return alertText;
}
public function getAlertYesno ():Boolean
{
return yesno;
}
public function getAlertTarget ():Object
{
return target;
}
/*
* Function to send data to the server. This function is triggered by
* the viewer by for example using the submit button. The for.. loop
* catches arrays for the variable names and for the variable values
* which come from textfields or comboboxes etc. The variable identifiers
* can be set in the XML file.
*/
public function sendData (datArray:Array, varArray:Array, dataFile:String):void
{
var myURLVariables:URLVariables = new URLVariables();
for (var count:int = 0; count < datArray.length; count++)
{
myURLVariables[varArray[count]] = datArray[count];
}
var myLoader:LoadData = new LoadData();
myLoader.initLoader (dataFile,completeHandler,null,myURLVariables);
}
/*
* Serverresponse. In this function all what is done is sent a response
* back to the viewer depending on what comes frome the server, an 'Okay"
* or an "Error" message.
*/
private function completeHandler (event:Event, a:Object, b:Object, c:Object):void
{
if (event.currentTarget.data.myResult == "Okay")
{
update("serverResponse");
}
else
{
update("serverError");
}
}
/*
* This is the setter function for any response from the server.
* We assume that in case of a positive response a new URL will be called.
*/
public function serverResponse(url:String):void
{
var request:URLRequest = new URLRequest(url);
navigateToURL(request,"_self");
}
/*
* This function always updates the viewers. It can handle several
* different event handlers.
*/
protected function update (evtType:String):void
{
dispatchEvent (new Event(evtType));
}
}
}