views:

72

answers:

2
A: 

Create a variable inside your View class called:

var main:Main;

Then a function to receive an object of type Main that sets the variable you created above, like this:

public function setMain(mainIN:Main):void
{
   main = mainIN;
}

Now you have a local copy of all the data contained in your main document class in your View class. You can access properties of main by doing this (inside your view class' functions):

main.model.x = mouseX; //just an example. For this, your model variable inside Main would need to be public.

To do data passing the other way, you simply create public properties or functions inside your View class, and then because you've created the instance of View inside your Main class, it will be able to access it as normal with code like:

view.someViewFunction();

In this way each class has access to each other's properties and functions. I hope this helps!

debu
I would avoid this. View should not know about main since it is now tightly coupled. If the structure of main changes then view will have to be updated too. At the very least if view needs something done then dispatch an event and have main listen for it :)
Allan
Here's the scenario :: Main.as is document root of a swf and it's being loaded in by another swf (the loader). When the View has completed doing it's job. The View needs to tell the Main.as to fire an Event.COMPLETE to the parent swf (the loader). In addition, the view needs reference to the Main.as to reference some of the display objects in the timeline.
Having your View class communicate with the object which has loaded the one which contains Main and View is tricky; I believe it is possible, but I've never done it. That loader object will fire an Event.COMPLETE object when it's finished loading in the new swf, though. As for having View have access to objects from Main, the method I've proposed above may not be idea as Allan says, but in practice it works very nicely, and I've not noticed any issues with memory. Do that, and then you can access Main's objects with something like main.someObject.x = someX;
debu
+1  A: 

I generally handle communication by changing properties in the model via the controller. On change of values in the model, i will dispatch events representing those changes. Anyone (Main in this case) that has a reference to the model can subscribe to those events. This results in a more circuitous implementation, but to a very loosely coupled result.

jordanx