2

I have a 'notifications app' in my program. In the app there is an 'notices' section. Each notice has an option to delete the notice with a delete button on that row. The 'notices' section also has a clear button. I want to make that clear button delete all of the notices in the 'notices' section. However I am having trouble doing this.

CONTROLLER

(function () {
'use strict';

angular
 .module('notifications')
 .controller('NotificationsCtrl', NotificationsCtrl);

function NotificationsCtrl($scope, $state, SecService, Service, RecService) {
  var vm = this;
  vm.ctrlName = 'NotificationsCtrl';

  $scope.showAppsMenu = false;

  $scope.appList = {};

  $scope.dismissNotification = function(notification) {
    EcosystemService.dismissNotification(notification.id).then(
      function (response) {
        delete     $scope.appList[notification.appId].notifications[notification.id];
      });
  };

  $scope.dismissAppNotifications = function(app) {
var notifications = app.notifications;
for (var i = 0; i < notifications.length; i++) {
  EcosystemService.dismissNotification(notifications[i].id).then(
    function(index) {
        return function (response) {
          delete app.notifications[index];
        }
    }(i));
}
};

I need to make a change in $scope.dismissAppNotifications. I am just stumped on what I need to add. The 'alerts' are not being cleared still. I added where the Service is being used. Maybe I need to add something here as well?

1 Answer 1

3

If I understand you correctly - you want to delete all notifications within one app - so iterating over appList is not necessary. Iterate over one app's notifications instead:

$scope.dismissAppNotifications = function(app) {
    var notifications = app.notifications;
    for (var id in notifications) {
      // may be reasonable to add checking for only own properties
      EcosystemService.dismissNotification(notifications[id].id).then(
        function(notificationId) { // outer function needed because of the way scopes and closures works in js
            return function (response) {
              delete app.notifications[notificationId];
            }
        }(id));
    }
};

notifications[id].id can be replaced with simply id if key and id property are the same.

Sign up to request clarification or add additional context in comments.

6 Comments

I understand what you did there. I tried to correct my code and then run it, but it still isn't deleting all the 'alerts'. When I click the button nothing happens. I debugged, but still can't seem to determine what is wrong with the code...
Does dismissing single notification work? Notifications property is an array or object?
Can you add console.log(JSON.stringify(app))) at the beginning of the function and tell me what's printed in browser's console? Also, what's notification.id - is it index of the notification in the list?
I also add a picture from when I was debugging.
Ok, now I see that notifications is an object and I'm iterating over it like it was an array. I'll update my answer in few minutes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.