Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think maybe you have over thought the problem. I do this sometimes.</p> <p>Your view class has to have some type of link to the model obviously and an event is a great way to do it. Something bare bones here to give you an idea.</p> <pre><code> // Model class package { class MyModel extends EventDispatcher { // you can make them public but that would // be against some oop practices. so private it is private var m_position:Vector2D; MyModel(){} // one way of doing getters/getters // example: theModel.SetPosition(something); public function GetPosition():Vector2D { return m_position; } public function SetPosition(value:Vector2D):void { m_position = value; ModelChanged(); } // the other way // sample: theModel.position = something; public function get position():Vector2D {return m_position; } public function set position(value:Vector2D):void { m_position = value; ModelChanged(); } private function ModelChanged():void { dispatchEvent(new Event(Event.CHANGE)); } } } // now for our view. package { class MyView extends Sprite // or whatever { private var model:MyModel; MyView(model:MyModel) { this.model = model; model.addEventListener(Event.CHANGE, handleModelChanged); // fire off an event to set the initial position. handleModelChanged(NULL); } private function handleModelChanged(evt:Event):void { x = model.position.x; y = model.position.y; // etc etc etc. } } } </code></pre> <p>Anyhow you don't need the setters if your going to have the logic in the model file also obviously if nothing outside of the model needs to change it no reason for setters. But you do need the getters.</p> <p>This decouples the model from the view and you can write any view any way you want and all you have to provide is a handler for when the model has changed. Just expose whatever data your views will need with getters.</p> <p>You now only have to loop through the models and if one changes it will fire off an event and the views that are listening in will update.</p> <p>hope I didn't miss anything and that explains what you were wanting.</p> <p>Edit: I forgot to add, you don't have to have "ModelChanged()" all over the place if your using something like an update function. Just update and when your finished fire off the event.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload