No subject

vishwas.gagrani's Avatar

vishwas.gagrani

11 Nov, 2012 07:35 PM

I wanna catch the events dispatched by Service class, inside the command class ( initiating the service). However, as the Service class is extending the class Actor ( not EventDispatcher), and thus using Actor's function called dispatch ( and not dispatchEvent). I am not able to use service.addEventlistener in command class.
What should i do in this case ( please also see the starred lines below for reference )

public class ConfigureDatabaseCommand extends Command
{
     [Inject]
              public var statsModel:StatsModel ;

    [Inject]
    public var service:XMLLoaderService ;

     override public function execute():void
    {
        trace("ConfigureDatabaseCommand>>>>>>>>>")//, e_Cde.body.test["xyz"] ) ; 
            service.execute(statsModel.INIT_XML_FILE_NAME );

       ///*********************************************************************************************************************************
            service.addEventListener( ServiceEvent.ON_COMPLETE, service_ON_COMPLETE ) ;
                            service.addEventListener( ServiceEvent.ON_ERROR, service_ON_ERROR ) ;
      ///*********************************************************************************************************************************
    }
}

}

  1. Support Staff 1 Posted by Joel Hooks on 11 Nov, 2012 07:45 PM

    Joel Hooks's Avatar

    IMO your best bet is to make a new command. Commands are meant to be stateless singular actions. As soon as they start waiting for a response, they are doing too much work.

  2. 2 Posted by vishwas.gagrani on 12 Nov, 2012 06:55 AM

    vishwas.gagrani's Avatar

    You mean, catching the ServiceEvent dispatches in "context" and then mapping a new command there ?

  3. 3 Posted by vishwas.gagrani on 12 Nov, 2012 01:48 PM

    vishwas.gagrani's Avatar

    That approach would be difficult. As services like XMLLoader are commonly used in so many views. Then how come i can bind an event like Service.LoadComplete to some command in the context.

  4. 4 Posted by Paul Robertson on 12 Nov, 2012 04:15 PM

    Paul Robertson's Avatar

    Listen for the native event within your service class, then dispatch a
    custom event for the success or failure of that particular service. If
    you need to do something to your model and/or service layer when the
    service returns, map commands in the context for the custom event. If
    you want to do something in the view when the service returns, map a
    listener in a mediator for the custom event.

    There are also alternatives, although what Joel is recommending is
    closest to what I would call "traditional" Robotlegs MVCS.

    Other people have come up with mechanisms for listening to an event in
    a command -- in this thread, item #8 I talk about some of the
    approaches people use:
    http://knowledge.robotlegs.org/discussions/questions/255-lifecycle-of-a-command

    One key bit is to register a listener in the command's execute()
    method, then call the commandMap's detain() method to keep the command
    from getting garbage collected:

    public function execute():void
    {
        myService.start();
        myService.addEventListener(MyServiceEvent.COMPLETE,
    completeHandler);
        commandMap.detain(this);
    }

    In your listener, once you've gotten a response, call the commandMap's
    release() method to free the command for garbage collection:

    private function completeHandler(event:MyServiceEvent):void
    {
        myService.removeEventListener(MyServiceEvent.COMPLETE,
    completeHandler);
        commandMap.release(this);
    }

    Note that I would still recommend using a custom event dispatched from
    your service, and I would still recommend *against* doing additional
    work on models etc. in the event handler -- since then your command
    does two things, not just one.

    Paul

  5. 5 Posted by vishwas.gagrani on 12 Nov, 2012 05:20 PM

    vishwas.gagrani's Avatar

    "Listen for the native event within your service class, then dispatch a custom event for the success or failure of that particular service. If
    you need to do something to your model and/or service layer when the
    service returns, map commands in the context for the custom event"

    I am having a service called "XMLLoader" . How can i map XMLLoader.Complete to a command "InitAppCommand" ? It does not look sensible because
    => XMLLoader service can be used many times in my application in future. And if i map it to InitAppCommand ( which is intended to run only once, on app startup), it will call the command again and again ( whenever used )

    ( I am just trying to follow the traditional method. Can't take risk of using detain() or something new :) )

  6. 6 Posted by Paul Robertson on 12 Nov, 2012 05:47 PM

    Paul Robertson's Avatar

    Well, I'm having to infer a lot about the structure of your app, so
    obviously my responses have limitations.

    It looks like your service does multiple things so I would split it up so
    the separation is more defined. It looks like you have a service.execute()
    method that is used for multiple operations. If one of those is a
    one-time-only setup task, I would have a separate method on the service
    just for that operation (or, if you really want separation, make a separate
    service class for each operation). Have one custom event that the service
    class dispatches when the "setup" operation is complete, and others that
    are dispatched when each other type of operation completes.

    Also, it is possible to set up a command mapping that's only fired once, by
    setting the fourth parameter ("oneShot") of commandMap.mapEvent() to true.
    I like to do that for all my startup commands so they automatically get
    unmapped once they've fired.

    Paul

  7. 7 Posted by vishwas.gagrani on 12 Nov, 2012 06:23 PM

    vishwas.gagrani's Avatar

    hmm...
    I think, it's good to go for the commandMap.mapEvent()'s 4th parameter.

    Thankyou.
    Vishwas

  8. vishwas.gagrani closed this discussion on 12 Nov, 2012 06:23 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac