Announcement Announcement Module
Collapse
No announcement yet.
Multiple Commands for one class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple Commands for one class

    Hi,

    I'm new to Spring Actionscript and I haven't really found a way to do the following:

    I have one class, for example LoginCommand.

    I want this class to be able to handle multiple types of events, for example:

    Code:
    [Command(eventType="submitLogin", executeMethod="submit")]
    [Command(eventType="clearLogin", executeMethod="clear")]
    public class LoginCommand
    {
    public function clear(event:Event):void
    {}
    
    public function submit(event:Event):void
    {}
    }
    Is this possible?
    I haven't found any example in the reference guide whatsoever.

    Also, on a sidenote, what do I have to add to LoginCommand to allow it to send MVCEvents itself?

    Greetings,
    Bart

  • #2
    Solved it myself.

    It worked afterall, even the sending of MCVEvents.

    The problem was that I have a class with a static method that dispatches events (for classes that don't inherit from EventDispatcher) and that wasn't tagged with [RouteMVCEvents]

    Comment


    • #3
      good going

      Hey Bart,

      glad to see you managed to solve everything by yourself, if you have any more questions feel free to drop them here. Good luck with your project.

      cheers,

      Roland

      Comment


      • #4
        I have some other questions regarding this:

        I have 2 classes:

        Code:
        [Command(eventType = Constants.E_LOGIN_SHOW, executeMethod = "showLogin")]
        [Command(eventType = Constants.E_INIT_APPLICATION, executeMethod = "initApplication")]
        [Command(eventType = Constants.E_ALERT, executeMethod = "alert")]
        [Command(eventType = Constants.E_REMOTE_FAULT, executeMethod = "handleRemoteFault")]
        public class ApplicationCommand extends BaseCommand
        {
        //...
        }
        Handles general application logic (such as showing the main content, etc..).

        Code:
        [RouteMVCEvents]
        [Event(name = Constants.E_VIEW_DASHBOARD, type = "com.docbyte.epasuite.client.event.ApplicationEvent")]
        [Event(name = Constants.E_ALERT, type = "com.docbyte.epasuite.client.event.ApplicationEvent")]
        [Event(name = Constants.E_REMOTE_FAULT, type = "com.docbyte.epasuite.client.event.ApplicationEvent")]
        [Command(eventType = Constants.E_LOGIN_CLEAR, executeMethod = "clearLogin")]
        [Command(eventType = Constants.E_DO_LOGIN, executeMethod = "doLogin")]
        public class LoginCommand extends BaseCommand
        {
        //...
        }
        This class handles actions related to authentication.


        Question 1: Is it ok to put so many [Command]-tags on one class?

        Question 2: Is it ok to let one class route the events aswell as listen to other events (ie. LoginCommand)

        If this is not the best approach for this, please explain how I should approach this problem.


        The reason I ask is because of the following:

        Currently, the application follows this trail of events:

        1: startup

        2: Main.mxml dispatches E_INIT_APPLICATION event, caught by ApplicationCommand (init of some values)

        3: Main.xml dispatches E_LOGIN_SHOW event, caught by ApplicationCommand (login window is attached to Main and shown)

        4: when clicked on submit, Login.mxml dispatches E_DO_LOGIN event, which is caught by LoginCommand

        5: LoginCommand handles the login, if successfull, E_VIEW_DASHBOARD event is dispatched by LoginCommand => This should be caught by ApplicationCommand (see class-tags), but this is only true 50% of the time.

        When I run my app, there's a 50% chance step 5 will always work, or a 50% chance that it will never work. (It either works all the time, or never.)
        This occurs without changing any code. Just running the same app 20 times will give +/-10 a working loginhandler, +/-10 times a failing loginhandler

        It seems for some reason ApplicationCommand is not picked up as a listener for that event sometimes.

        If I make a new class and attach the Command-tag for that event to my new class and remove it from ApplicationCommand so that new class will listen for that event, it always works (tried running the application 20 times, never any problems).


        Greetings,

        Bart
        Last edited by thierensb; Nov 18th, 2010, 07:11 AM.

        Comment


        • #5
          ///

          Hey there,

          as for your questions:

          #1. It shouldn't be a problem to put that many [command] tags on one class.
          #2. That's a tough one. I think in general you'd be better off splitting this functionality because the event/command separation is meant to facilitate a separation of concerns. In the case of your login class, which has quite a simple flow of logic its tolerable to stick everything in one class. But in strict OOP terms, no, what you're doing is wrong

          As for the code example you're posting, there is one amjor issue with this, this code is faulty:
          Code:
          [Command(eventType = Constants.E_LOGIN_SHOW, executeMethod = "showLogin")]
          The Constants.E_LOGIN_SHOW name will *not* be resolved at compile time, I'm afraid. In actionscript metadata you have no choice but to put in the string literals, so the code in your previous example *is* correct:

          Code:
          [Command(eventType="clearLogin", executeMethod="clear")]
          The first code example will effectively make all the command methods be invoked for every event, I think, because quite probably all keys will have an empty value.

          hope that helps,

          Roland

          Comment


          • #6
            Thanks for the input.

            I changed the Constants.xxx to the actual String values but the application still behaves the same way.

            Also, if it's not optimal to let for example LoginCommand listen to events aswell as send/route them, how do I have to do the following case:

            - LoginCommand receives the doLogin-event
            - LoginCommand tries to logs in and gets the result from the backend
            => here LoginCommand (or something else) needs to send an event to the ApplicationCommand to either show a popup when the login failed, or show the actual application.
            If I can't tag LoginCommand as [RouteMVCEvents] how do I approach this issue?

            Thanks,

            Bart

            Comment

            Working...
            X