如果没有使用JavaScript连接到应用程序的通知,则删除应用程序

Remove Applications if there are no Notifications connected to the Apps using JavaScript

本文关键字:应用程序 通知 删除 JavaScript 连接 如果没有      更新时间:2023-09-26

我有一个"通知"应用程序,它在其中显示不同的应用程序,这些应用程序可能有关联的通知。如果应用程序没有通知,则只显示应用程序本身。加载应用程序后,我希望检查是否有与应用程序相关的通知。如果不存在通知,那么我希望这些应用程序不被显示或删除。我不确定我的代码出了什么问题。

我认为我需要在listenForNotificationsgetApps中添加一些代码来检查是否有,如果没有,则"删除"应用程序。getApps首先被击中,并拉入用户可能拥有的应用程序。所以,也许可以在那里查看是否有任何通知与该应用程序相关?我以为我走在了正确的轨道上,但它仍然不起作用。

控制器

function NotificationsCtrl($scope, $state, Service, SecService, RecService) {
var vm = this;
vm.ctrlName = 'NotificationsCtrl';
$scope.showMenu = false;
$scope.appList = {};
$scope.dismissNotification = function(notification) {
  EcosystemService.dismissNotification(notification.id).then(
    function (response) {
      delete $scope.appList[notification.appId].notifications[notification.id];
    });
};
$scope.dismissNotifications = function(app) {
  var notifications = app.notifications;
  for (var id in notifications) {
    EcosystemService.dismissNotification(notifications[id].id).then(
      function(notificationId) {
        return function (response) {
          delete app.notifications[notificationId];
        }
      }(id));
  }
};
$scope.init = function () {
  Service.getUserAppsByEmail(SecService.secState.username).then(function (response) {
    var apps = response.applications;
    $scope.appList['notes'] = {
      name: 'Notes',
      notifications: {}
    } ;
    for(var i = 0; i < apps.length; i++) {
      $scope.appList[apps[i].appId] = {
        name: apps[i].name,
        icon: apps[i].icon,
        notifications: {}
      }
    }
  });  
};
function listenForNotifications() {
  RealtimeService.on("notifications", function (data) {
    var app = $scope.appList[data.body.appId];
    if (app != null) {
      if(!data.body.dismissed) {
        data.body.unixEpoch = Date.parse(data.body.whenCreated);
        app.notifications[data.body.id] = data.body;
      }
      else {
        delete app.notifications[data.body.id];
        delete app.appList[data.body.appId];
      }
    }
    else {
      delete app.appList[data.body.appId];
    }
  });
}

您可以执行以下操作:

<div ng-repeat="app in appList" ng-if="app.notifications.length > 0">
     .......
</div>
<div ng-if="noNotifications()">No notifications!</div>
$scope.noNotifications = function() {
    var foundNotifications = true;
    angular.forEach($scope.appList, function(app, index) {
        foundNotifications = foundNotifications && app.notifications && app.notifications.length > 0;
    });
    return !foundNotifications;
};

编辑:我添加了一个更简单的解决方案,通过检查$scope.noNotifications()函数来显示"无通知"。

这就是最终解决问题的原因。。。将其添加到HTML 中

<div ng-repeat="app in appList" ng-show="(app.notifications | AssociativeArrayFilter) > 0">

并将其添加到控制器中

angular.module('notifications').filter('AssociativeArrayFilter', function () {
return function (collection) {
  if (!collection) {
    return 0;
  }
  var reCount = 0;
  angular.forEach(collection, function (val, index) {
    reCount++;
  });
  return reCount;
  }
})