Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
Old 09-05-2007, 02:30 AM   #1
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,140
Default MVC: When should the view change itself?

Hey Guys,

In using MVC, when is it appropriate for the View to change its own status? When is it appropriate for the Controller to change the View?

For instance, lets assume we have a volume slider with those little volume bars that change when you slider (just like M$ media player). When the slider is moved in the View, it tells the Controller to change the volume in the Model. But who's job is it to change the volume bars showing in the View? The View could easily do this on it's own. But is that appropriate? Who's job is it to update the View and when?
__________________
trace("Good bye Flash.") Log.i(TAG, "Hello Droid");
Flash Gordon is offline   Reply With Quote
Old 09-05-2007, 08:51 AM   #2
jsebrech
Joeri Sebrechts
 
Join Date: Apr 2005
Location: Antwerp, Belgium
Posts: 1,465
Default

Generally, you should never have to "explicitly" put the view in the right state. Anything that changes should trigger events or call code automatically that causes the view to update itself.

Whether the view should listen only to the controller or also to the model is a matter of debate, so do whatever seems right to you.

In this specific case, i'd put a volume property on the controller that triggers events that the view's volume bar listens to for updates. After that changing the volume from code or from gui has no influence on your view's behavior.
jsebrech is offline   Reply With Quote
Old 09-05-2007, 03:45 PM   #3
Assertnfailure
as[org].addListener(this)
 
Assertnfailure's Avatar
 
Join Date: Dec 2005
Location: LA, California
Posts: 837
Default



The way I interpret it is that the controller is meant to act on behalf of all the views when talking too the model, but also is responsible for controlling the way views are represented (beyond the scope of the views themselves) within an application.

So in other words, if you want to display the current volume, the volume bar should be able to do that on its own based on the model as well as its own internal implementations. If you want to show or hide the volume elements, then, as a single view, the controller can opt to add or remove it from the display list.

However, keep in mind that views in practice usually aren't too reusable outside of the particular project they're built in. Typically, they are rigidly constructed to account for the specific model they are using. Because of this, I would turn the volume element into a UI component that could be composed into a view, rather than being a view itself.
Assertnfailure is offline   Reply With Quote
Old 09-06-2007, 07:12 AM   #4
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,140
Default

Thanks for the help guys! I'm really enjoying learning about and discussing all of this OO stuff!

Here is what I got for my design so far:


As you can see, all the UI objects are decoupled from the view, so hopefully I do have that going for me. Or maybe I'm on the wrong track and this isn't really a MVC, I don't know.

I kind of see the volume bars like a rollOver or Press of a button. If you were to dynamically add an effect for when a button in the View was pressed, obviously there is no update from the Model (because we are listen to onRelease) so it can't get that command from there. That leaves to Controller to implement the press effect or the View itself. I would probably make the View responsible for its own actions when the actions don't serve any real purpose except to look nice (as in the volume bars). However, I'm just a noob trying to learn.

What do you guys think of that above paragraph?
__________________
trace("Good bye Flash.") Log.i(TAG, "Hello Droid");

Last edited by Flash Gordon; 09-06-2007 at 07:40 AM.
Flash Gordon is offline   Reply With Quote
Old 09-06-2007, 09:18 AM   #5
jsebrech
Joeri Sebrechts
 
Join Date: Apr 2005
Location: Antwerp, Belgium
Posts: 1,465
Default

The end goal of a good MVC is this:
- There are many models
- There are many views, all tied to a particular model. Some views may show the same model.
- There is 1 or more controller, which can move from view to view, and can be used to interact with the view / view's model.

In concrete terms, for an image editor you would have many Image models, many ImageViewer views, some of them showing a different view of the same Image, and a central controller that knows about all the Image instances, and when a view is activated will tie itself to that ImageViewer / Image. The controller does not need to be involved in updates of the model that trigger view changes (although there are people that are of the persuasion that controllers need to shield the model from the view and vice versa).

Now, for a video app things get complicated because I don't know if flash allows you to have multiple views of the same video (haven't used video at all in flash).

You're right when you say that the volume bars are purely aesthetic. All your view should do is listen to the model for volume changes, and send messages to the controller (or to the model, depending on design) to change the volume. Interactions that don't change the model should be strictly within the view (although they may be driven from the controller).
jsebrech is offline   Reply With Quote
Old 09-06-2007, 04:15 PM   #6
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,140
Default

Well....for the video thing. I actually have the real video inside the model and the model is added to the display list. Since the video IS the actual data in this case, I though that would be okay. The view then is just everything else, like the play buttons, volume, progress bars, etc.... I don't know if that is typical or not. But I do see the video as being the actual data in this case.

thoughts?
__________________
trace("Good bye Flash.") Log.i(TAG, "Hello Droid");
Flash Gordon is offline   Reply With Quote
Old 09-07-2007, 01:02 PM   #7
jsebrech
Joeri Sebrechts
 
Join Date: Apr 2005
Location: Antwerp, Belgium
Posts: 1,465
Default

Yes, I suppose that's probably the best way to organize it. "In theory" though, the model is non-visual.
jsebrech is offline   Reply With Quote
Old 09-07-2007, 03:00 PM   #8
Assertnfailure
as[org].addListener(this)
 
Assertnfailure's Avatar
 
Join Date: Dec 2005
Location: LA, California
Posts: 837
Default

Hmm...in the case of the video...I would probably just store the video's URL in the model, and load the video into a view.

Model should never need to be a display object.

Also, one other thing with your class listings...
The view should have no direct association to any controller, but rather should communicate to the controller via events. (Hence the dotted line from view to controller in the image I attached).

What tool do you use to generate your image, by the way?
Assertnfailure is offline   Reply With Quote
Old 09-07-2007, 05:32 PM   #9
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,140
Default

Yea I think you guys are right about that. With more thought, i think it could be better organized. NetStream is the actual data (that will be in the Model) and Video is just the display. I'll move the Video class to the View and that will take care of the Model so it is no longer a display object.....i think that will work.

Quote:
Originally Posted by Assertnfailure View Post
Also, one other thing with your class listings...
The view should have no direct association to any controller, but rather should communicate to the controller via events. (Hence the dotted line from view to controller in the image I attached).
Hmm....are you saying the View should communicate with the Controller the same was the Model communicates with its Views?


Quote:
Originally Posted by Assertnfailure View Post
What tool do you use to generate your image, by the way?
Photoshop.
__________________
trace("Good bye Flash.") Log.i(TAG, "Hello Droid");
Flash Gordon is offline   Reply With Quote
Old 09-11-2007, 05:46 PM   #10
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,140
Default

I think i got it. The Controller should be a observer of the View just as the View is an observer of the Model.

I don't have my Java OO book anymore (had to return it to the library) so I can't consult that, but what are the advantages of "Controller should be a observer of the View"?
__________________
trace("Good bye Flash.") Log.i(TAG, "Hello Droid");
Flash Gordon is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Change color of text qu1en ActionScript 2.0 2 07-14-2008 05:33 PM
How do I change ComboBox color? retrogamer1991 Components 2 04-21-2006 09:15 PM
Load Variable to dynamically change image vividbeatz ActionScript 2.0 2 11-03-2005 04:44 AM
Checkbox component colour change? 101_North Components 4 10-31-2005 09:06 AM
change handler not working!!! finnstones ActionScript 1.0 (and below) 0 09-04-2003 11:12 AM


All times are GMT. The time now is 01:31 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.