0

I have a Command that executes a service call. In the result handler, I am doing some logic based off the result data. If the logic meets specific criteria, I am displaying a confirmation popup. If the user clicks the continue button in the confirmation popup, I have a method that gets called, which dispatches a Parsley event. That Parsley event is not being caught. However, if I dispatch the Parsley event from inside the result method, it is being caught. Any idea why the event is not being caught when dispatching it from outside the result method?

For example...

[MessageDispatcher]
[Bindable]
public var dispatcher:Function;

I execute some service call from inside the command:

public function execute(event:SomeEvent):AsyncToken
{
return service.callService(event.type, false);
}

I now have a result handler like this:

public function result(data:Object):void
{
if (add some logic here based off data)
AlertHelper.showContinueQuestion(onSelection, "Are you sure you want to continue?");
}

If the user clicks the Continue button on the confirmation popup, it calls the onSelection method:

private function onSelection():void
{
dispatcher(new SomeEvent(SomeEvent.UPLOAD));
}

That Parsley event, SomeEvent, is not being caught. However, if I dispatch that event after the if statement, it is being caught and works fine. Any idea why it is not being caught when dispatched from outside of the result handler? I tried in other commands too, and it does the same thing.

anad2312
  • 787
  • 2
  • 8
  • 20
  • Can we see your handler that should be catching SomeEvent, and where it is attached? – Andrew Arace Apr 18 '13 at 17:59
  • This probably has something to do with the dynamic command being removed from the parsley context as soon as the result handler has finished executing. Though it's strange, I would have guessed that your injected [MessageDispatcher] would still be able to dispatch the event... can you confirm that the `onSelection()` method is getting invoked and that `dispatcher` is not null? I usually use a separate command to trigger the alert ... that is, in your result handler you dispatch a "show alert" event. The "show alert" command could then return an `AsyncToken`, and have its own result handler. – Sunil D. Apr 18 '13 at 18:04
  • Andrew, I am using a config file that contains the DynamicCommand: – anad2312 Apr 18 '13 at 19:02
  • Sunil, I think that is the case. The onSelection does get called. I think it has to do with what you stated because of the way the AlertHelper class works. The onSelection in the AlertHelper line of code (see above) seems to call onSelection before the rest of the code completes. Weird! Anyway, I ended up updating a flag in the model, instead of dispatching an event, and in my PM I have a BindSetter listening for changes to that flag (Boolean). Not sure I like using a BindSetter. Any other ideas? Thanks for the info. – anad2312 Apr 18 '13 at 19:05
  • Sunil, I just checked and dispatcher is null in the onSelection method. Not sure how it is being removed from the parsley context. Weird! – anad2312 Apr 18 '13 at 19:14

1 Answers1

1

Found this on the Spicefactory site, works as designed. I ended up updating a flag in the Model, versus dispatching an event. I then have a BindSetter listening for changes to that flag in the model. When the flag is set to true, the Parsley event is dispatched.

Command Object Lifecycle

Apart from grouping the executing method and the result handlers the DynamicCommand also introduces a special kind of lifecycle management for command objects. The creation of the object does not happen until a matching message is dispatched. It then becomes a container managed object just for the duration of the command execution. It will immediately be removed from the Context after the result or error handler have been invoked. But during its lifetime, it is a fully managed object, can have its dependencies injected, or even take part in messaging during the command execution. But that would be a rather rare case, most common usage scenario is probably the command object just receiving all the dependencies it needs to execute the command.

anad2312
  • 787
  • 2
  • 8
  • 20