yukatan / commangular

Command framework for AngularJS
MIT License
83 stars 8 forks source link

Commangular returns Command to .then functions #12

Closed mscharp closed 9 years ago

mscharp commented 9 years ago

I have a command returning a promise:

commangular.create( 'SomeCommand', function ( $log )
{
    return {
        execute: function ()
        {
            return $timeout(function() {
                               return {message: "I'm done"};
                           }, 1000);
        },
        onResult: function( result )
        {
            $log.debug( 'onSuccess:: message: ' + result.message );
        },
        onError: function ( e )
        {
            $log.error( 'Error: ' + e.message );
        }
    }
}, { resultKey: 'result' } );

and a function in an angular controller:

$commangular.dispatch( Events.SOME_EVENT ).then( function( response )
{
        // i want to access like this
    console.log('onSuccess:: ' + response.message );

       //instead i have to access like this
       console.log('onSuccess:: ' + response.result.message);
} );

I would expect that when my onSuccess function is called, the response parameter would be the unwrapped promise result from the command. However, when I inspect the response parameter it appears to be a command object of some sort. It has the following structure:

{
    commandModel: Object
    lastResult: Object
    result: Object
}

Is this intentional? Why doesn't it just return the result to the onSuccess function?

Thanks

yukatan commented 9 years ago

Hi, The result you get from the dispatch call is the expected result. You are executing just one command, but in commangular when you dispatch and event you can execute a command chain with decision points and other features, so the response from the dispatch can't be the result from just one command.

In a normal commangular application even you don't need to capture the result from the dispatch. You should use the onResult method inside the command to get the result from the promise resolved. If you need to execute something after all the command chain you can use event aspect "@After" and inject any of the result keys from any command executed in the chain. if some result key was a promise, the result resolved will be injected

mscharp commented 9 years ago

Thank you for the quick response! That makes perfect sense as to why it's not the resolved promise.

The reason I'm capturing the result is because I need the result from the async call in the context from which the dispatch was made.

yukatan commented 9 years ago

If you need to do it like that, then read the command result from the commandModel property using the result key. Al the results resolved are there. It should work.

mscharp commented 9 years ago

Great thanks!