What's the best way to reuse functions in a Flex MVC framework?

I am using MVC Cairngorm architecture for my current project.

I have several commands that use the same type of function that returns a value. I would like to have this function in one place and reuse it, not duplicate code in every command. What's the best way to do this?

0


source to share


3 answers


Create a static class or static method in one of the Cairngorm classes.

class MyStatic
{
    public static function myFunction(value:String):String
    {
        return "Returning " + value;
    }
}

      

Then where do you want to use your function:



import MyStatic;

var str:String = MyStatic.myFunction("test");

      

Another option is to create a top-level function (a la "trace"). Check this post, I wrote here .

+1


source


You have many options here - publicly defined functions in your model or controller, for example:

var mySharedFunction:Function = function():void
{
   trace("foo");
}

      



... static methods for new or existing classes, etc. The best practice probably depends on what the function is supposed to do. Can you clarify?

+1


source


Create an abstract base class for your commands and add your function to the protected area. If you need to reuse it elsewhere, reformat it into a public static method in the utility class.

+1


source







All Articles