christianalfoni / flux-angular

Use the FLUX architecture with Angular JS
313 stars 50 forks source link

Calling an action in emitChange() handler. #18

Closed oleduc closed 9 years ago

oleduc commented 9 years ago

Hello this is a quick question.

How does one go about calling an action in a emitChange() handler without getting the "Error: Cannot call dispatch while another dispatch is executing. Attempted to execute 'some-action' but 'some-other-action' is already executing." error.

Example:

        $scope.$listenTo(pageStore, function() {
            var previousPage = currentPage;
            currentPage = pageStore.getPage();

            switch (currentPage.status) {
                case 'trigger':
                    console.log('Rootscope -> Triggering page change!');
                    pageActions.change.attempt(currentPage.info.name');
                    break;
                case 'transitioning':
                    console.log('Rootscope -> Page change initiated!');
                    break;
                case 'accepted':
                    console.log('Rootscope -> Page change accepted!');
                    $rootScope.pageTitle = currentPage.info.title;
                    $rootScope.pageDescription = currentPage.info.description;
                    break;
                case 'rejected':
                    console.log('Rootscope -> Page change rejected!');
                    $state.go('login');
                    break;
                default:
                    console.log('Rootscope -> Unhandled pageStore event!', page);
            }
        });

Is this case, I would expect the action to be queued after the current dispatch instead of throwing. But my guess is that I don't quite understand how FLUX dispatch works.

Of course, one could always user a $timeout, but this is a pretty bad hack and I'm sure there must be a way to handle this.

Your help would be very much appreciated! And GREAT work on the library, we are very much appreciative of your efforts!

christianalfoni commented 9 years ago

Hi @oleduc ,

Without taking too much look, in loss of time at the moment, I would suggest trying to think of most of this logic as part of your store. You can access the $rootScope there to set page title and description. You could even argue doing the $state.go inside a store.

There you could return a call to a different store method (which you access by action here). That should solve the issue.

Sorry to be so short and abstract, I can take a closer look if you have not found a solution by the time I am back :-)

christianalfoni commented 9 years ago

Closed this, old issue