JanStevens/angular-growl-2

Impossible to send notifications to another AngularUI Router state

Opened this issue · 3 comments

DWand commented

Hello. I use the AngularUI Router in my application. I've found that it is almost impossible to send notifications to growl directives in other views. Here is a use case:

There is an application with 2 pages/routes/states.
The first page has the growl directive in a view: <div growl inline="true" reference="page1"></div>

  1. A user of the application goes to the first page.
  2. The user goes to the second page.
  3. A controller of the second page calls growl.success('Message', { referenceId: 'page1' });
  4. The user navigates to the first page.
  5. The user sees nothing.

As I understand, the problem is in this line of code:
https://github.com/JanStevens/angular-growl-2/blob/master/src/growlDirective.js#L18

When the user navigates to the first page again, the directive tries to initialize itself once more time and clears all its messages here:
https://github.com/JanStevens/angular-growl-2/blob/master/src/growlMessageService.js#L39

I have not dug deeply into the code and don't know why directives and preloadDirectives are separated. But this problem probably could be solved at least by preventing the directive from reinitialization (https://github.com/JanStevens/angular-growl-2/blob/master/src/growlDirective.js#L18):

if (!growlMessages.directives[$scope.referenceId]) {
  growlMessages.initDirective($scope.referenceId, $scope.limitMessages);
}

or by enhancing initialization logic (https://github.com/JanStevens/angular-growl-2/blob/master/src/growlMessageService.js#L32-L45):

this.initDirective = function (referenceId, limitMessages) {
    if (preloadDirectives[referenceId]) {
        this.directives[referenceId] = preloadDirectives[referenceId];
        this.directives[referenceId].limitMessages = limitMessages;
    } else if (!this.directives[referenceId]) {
        this.directives[referenceId] = {
            messages: [],
            limitMessages: limitMessages
        };
    }
    return this.directives[referenceId];
};

The preloading was a simple hack in case somebody tried to use the growl notifications and angular was still initialising the directive/controller. (recent bug of undefined messages).

I know it was a quick fix but I havent got the time to fix it properly, feel free to add a PR! thanks

DWand commented

@JanStevens, as for the PR - I'm not sure about a proper place for this fix and afraid to mess up the code.

DWand commented

@JanStevens, I've done some work with the code. You can see it here: https://github.com/DWand/angular-growl-2/tree/updates.