On 16/02/2012 12:04, tom rhodes wrote:
um, Ross gave it i think dude.
Yes, I posted a correction..
i didn't give any code examples :)
maybe you missed the smiley after i said "if you don't like it", perhaps
shoudl have been a ;). what problems do you have using assets mad ein the
IDE with mvc?
Ross' example passed the model and controller via the constructor. If
you build a view using the IDE and leave things sitting inside a MC or
the stage, you won't be calling the constructor, the IDE will write code
to instantiate those components for you, so passing the arguments can be
a problem.
There's also a school of thought that initialisation is best done in a
separate function separately to the constructor, so that an instance can
be re-initialised multiple times. That's more about OO than MVC.
Ross' example is superb. My small point is made for anyone coming new to
OO programing and trying to marry the two worlds of the timeline/code
development with MVC.
Ross' example for the view would be like this (allowing the IDE to
instantiate and the user to hook up the model and controller):
public function View(model:Model=null,
controller:Controller=null){
super();
if ( model != null){
init(model, controller);
}
}
public function init(model:Model, controller:Controller){
model.addEventListener(Event.CHANGE,
this.model_changeHandler);
this.model = model;
this.controller = controller;
this.addChild(this.textField);
this.stage.addEventListener(MouseEvent.CLICK,
this.stage_clickHandler)
}
I definitely agree that Ross' example is the most succinct MVC example I've
ever seen.
Top job.
On 16 February 2012 12:21, Paul Andrews<[email protected]> wrote:
On 16/02/2012 11:11, Paul Andrews wrote:
On 16/02/2012 10:54, tom rhodes wrote:
traditional vanilla MVC tends to use composition like the example given,
check out robotlegs if you don't like it :) then you inject dependencies
instead of passing them around and everything gets a bit more loosely
coupled...
It's not a question of not liking it - your example is perfect for a
coded-only solution, slightly less practical when working in a mixed-mode
with the Flash IDE being involved in view instantiation.
I should say "Tom's example"..
On 16 February 2012 11:46, Paul Andrews<[email protected]> wrote:
Excellent job.
I have one small point, on a practical level.
For the views, in particular, I don't usually pass arguments to the
constructor. It's not such a big deal for code-only examples, but in my
flash world I mix my components between stuff created dynamically and
things created in the IDE - sometimes I build components as assemblies
on
the stage using the IDE. The point is that passing arguments to the
constructor then becomes an issue for a view.
Might I suggest an init() function to pass in Model and Controller?
Paul
On 16/02/2012 07:59, Ross Sclafani wrote:
From: Ross Sclafani<ross.sclafani@gmail.****com<
[email protected]>>
Date: February 16, 2012 2:39:34 AM EST
To: Cor<[email protected]>
Cc: Flash Coders List<flashcoders@chattyfig.**f**igleaf.com<http://figleaf.com>
<flashcoders@**chattyfig.figleaf.com<[email protected]>
,
[email protected]
Subject: Re: MVC
feel free to hit me up any time ill try to have time to respong
none the less, this should get you started:
/*
* Model.as
*
* mvc.Model;
*/
package mvc {
import flash.events.EventDispatcher;
import flash.events.Event;
class Model extends EventDispatcher{
private var _count:int = 0;
public function get count():int{
return _count;
}
public function set count(value:int):void{
this._count= value;
update();
}
public function Model(){
super()
}
private function update():void{
this.dispatchEvent(new Event(Event.CHANGE));
}
}
}
/*
* Controller.as
*
* mvc.Controller;
*/
package mvc {
public class Controller {
private var model:Model;
public function Controller(model:Model){
super()
this.model = model;
}
public function countUp():void{
this.model.count++;
}
}
}
/*
* View.as
*
* mvc.View;
*/
package mvc {
import flash.text.TextField;
import flash.events.Event;
import flash.display.Sprite;
public class View extends Sprite(){
private var model:Model;
private var controller Controller;
private var textField:TextField = new TextField();
public function View(model:Model,
controller:Controller){
model.addEventListener(Event.****CHANGE,
this.model_changeHandler);
super()
this.model = model;
this.controller = controller;
this.addChild(this.textField);
this.stage.addEventListener(****
MouseEvent.CLICK,
this.stage_clickHandler)
}
private function stage_clickHandler(event:**
MouseEvent):void{
this.controller.countUp();
}
private function model_changeHandler(event:****
Event):void{
this.textField.text = this.model.count
}
}
}
/*
* App.as
*
* MVC screen click counter.
*/
package{
import mvc.Model;
import mvc. View;
import mvc.Controller;
public class App{
private var model:Model = new Model();
private var controller:Controller = new
Controller(this.model);
private var view:View = new View(model, controller);
public function App(){
this.addChild(this.view);
}
}
}
Now imagine a Model with more properties.
And tons of different Views of them that data.
Some of which provide a UI linked to Controller methods that manipulate
it.
_ross
______________________________**_________________
Flashcoders mailing list
[email protected].**com<[email protected]>
http://chattyfig.figleaf.com/**mailman/listinfo/flashcoders<http://chattyfig.figleaf.com/mailman/listinfo/flashcoders>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders