apache-flexmodel-view-controlleractionscriptcode-reusecairngorm

What is the best way to reuse functions in Flex MVC environment?


I am using a Cairngorm MVC architecture for my current project.

I have several commands which use the same type of function that returns a value. I would like to have this function in one place, and reuse it, rather than duplicate the code in each command. What is the best way to do this?


Solution

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

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

    Then where 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 out this post I wrote here.