JanStevens / angular-growl-2

growl-like notifications for angularJS projects
MIT License
285 stars 97 forks source link

wrong index initialization in destroyAllMessages function #66

Closed Siggen closed 9 years ago

Siggen commented 9 years ago

In version v0.7.3, the destroyAllMessages function is defined like this :

this.destroyAllMessages = function (referenceId) {
  var messages = this.getAllMessages(referenceId), i = messages.length;
  for (i - 1; i >= 0; i--) {
    messages[i].destroy();
  }
  if (this.directives[referenceId]) {
    this.directives[referenceId].messages = [];
  }
};

On the first loop, notice the i - 1 statement which does nothing. Calling this method on an empty messages array will iterate one time and will raise an exception because messages[0] returns undefined.

I propose the following version :

this.destroyAllMessages = function (referenceId) {
  var messages = this.getAllMessages(referenceId);
  for (var i = messages.length - 1; i >= 0; i--) {
    messages[i].destroy();
  }
  if (this.directives[referenceId]) {
    this.directives[referenceId].messages = [];
  }
};
royerboat commented 9 years ago

I submitted a pull request to fix this. Hopefully it gets picked up.

Siggen commented 9 years ago

Thanks a lot !

JanStevens commented 9 years ago

Nice catch, I'll look at the MR :+1:

Siggen commented 9 years ago

That was quick, thanks :-)