alexcrack / angular-ui-notification

Angular.js service providing simple notifications using Bootstrap 3 styles with css transitions for animating
MIT License
536 stars 169 forks source link

passing custom data to the notification scope #113

Closed mtomran closed 7 years ago

mtomran commented 7 years ago

How can I pass a custom data object to the notification scope? Something like options.data so it can be used in notification template.

alexcrack commented 7 years ago

Use scope option and custom templates (see examples folder):

...
$scope.data = {hello: "world"};
Notification.primary({message: "Just message", templateUrl: "custom_template.html", scope: $scope});
...
mtomran commented 7 years ago

Thanks for your response. The problem that I had with this way is it pollutes the source scope. Also, in my case, I have a list of items in the source scope that could trigger notification and each notification have a button that triggers an event based on an id that gets passed to the notification scope. My solution was to create a new scope and attach the id and pass it the notification scope and destroy the scope when done, but thought it might be nicer to use the already cloned scope of the notification and save the extra work.

alexcrack commented 7 years ago

Yes. Creating of a new scope is another way. You can create it.

...
let notificationScope = $scope.$new();
notificationScope.data = {hello: "world"};
Notification.primary({message: "Just message", templateUrl: "custom_template.html", scope: notificationScope});
...
// Don't forget to destroy that scope after to prevent memory leak.
notificationScope.$destroy();
mtomran commented 7 years ago

I realized that notification show returns a promise with notification scope. I am not sure if there is any drawback with this method, but a better way would be to attach any data or function to be consumed by the notification to that scope since it would be destroyed by the kill function.

Notification.primary({message: "Just message", templateUrl: "custom_template.html"})
.then(function(nScope){
  nScope.data= {hello: "world"};
  nScope.onClickFunc= function(){}
});
alexcrack commented 7 years ago

Destroying the notification's scope inside 'kill' function (also when notification is closing by timeout) is planned in the future release.