开发者

actionscript 3 MVC work flow

What's the best practice for passing data between the main class and the view class and vice versa?

main.as

var model : Model = new Model();

var view : View = ne开发者_运维技巧w View();

var controller : controller = new Controller();

public function callFromView() : void {}

view.as

// how to reference the main.as

private function callToMain() : void

{

// please help

}


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜