In an ARP type application, most of the setup is done in the onLoad method of
the Application view (which is a container for all
views).
In other words, BookView, ControlView, etc are children of Application.
Also make sure to initialize the EventDispatcher outside the class constructor,
through a static variable.
Static class properties are run before the constructor ;-)
I use a base Form class which all views extend, which looks something like this:
import mx.events.EventDispatcher;
class com.muzakdeezign.view.Form extends MovieClip {
// allow class to broadcast events
private static var dispatcherInit =
EventDispatcher.initialize(Object(com.muzakdeezign.core.Form.prototype));
// mix-in from EventDispatcher
var addEventListener:Function;
var removeEventListener:Function;
var dispatchEvent:Function;
}
Then the Application could look like this:
import com.muzakdeezign.view.Form;
class Application extends Form {
private var bookView:BookView;
private function onLoad():Void {
bookView.setModel(new PAModel());
}
}
import com.muzakdeezign.view.Form
class BookView extends Form {
private var __model:PAModel;
public function setModel(newModel:PAModel):Void {
this.__model = newModel;
this.__model.addEventListener("pagesUpdated",this);
this.__model.addEventListener("currentPageUpdate",this);
}
}
Now, with all that said, you might wanna look into a ModelLocator Singleton so
that view get the required model through the
ModelLocator rather then setting it from somewhere (Application in the above
example).
You can google "ARP ModelLocator" for more on that.
In short, instead of the setModel() method, the BookView would call:
ModelLocator.getInstance().getModel("PAModal");
which would return a reference to the PAModel (usually also a Singleton).
Does that make sense??
regards,
Muzak
----- Original Message -----
From: "Giles Roadnight" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Friday, June 01, 2007 4:53 PM
Subject: [Flashcoders] MVC question
> Hi All
>
> I am building my first MVC (Model View Controller) and have a question about
> how I should set it up.
>
> I have a load of classes: BookView, ControlView (both views), Controller and
> PAModel.
>
> Controller holds all the other classes and sets them all up. Both views have
> a function - setModel:
>
> public function setModel(newModel:PAModel):Void
> {
> this.Model = newModel;
> this.Model.addEventListener("PagesUpdated",this);
> this.Model.addEventListener("currentPageUpdate",this);
> }
>
> that sets the model (obviously) and sets up event listeners.
>
> If I try to fire this function in the controller constructor it doesn't fire
> - presumably the classes haven't woken up yet so I am firing it from a
> function that fires when XML has loaded. This doesn't seem like the best way
> of doing it though.
>
> What is the best way round this? I have tried firing a onInit function from
> the views when they are ready but this doesn't seem to work. I assume it's
> because the addEventListener function doesn't work when the controller tries
> to register as a listener.
>
> Thanks for the help.
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com