From 92602485434d17b473b2034b4291d29d869c4076 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 13 May 2018 07:44:19 +0200 Subject: [PATCH] gui: Fix scopes in notification directive (fixes #4941) (#4948) The directive lives in its own isolated scope (where we put the visible() function). The stuff transcluded into the notification directive lives in the root scope and doesn't have access to the directive scope. Hence we cannot call dismiss() from inside the directive. Similarly, config does not exist by itself in the directive scope, we need $parent to access the root scope. Reference: https://docs.angularjs.org/guide/directive#isolating-the-scope-of-a-directive How this worked before is a mystery. My guess is Angular bug with directive scope that was fixed in 1.3. One possibility is that transclude plus scope: true (which doesn't make sense as that is supposed to be an object) resulted in the root scope being used in the directive itself. This would then "work" as long as there is only one notification, as visible() and dismiss() would then be registered on the root scope, thus accessible from within the notification but also overridden by any notification rendered. --- gui/default/syncthing/core/notificationDirective.js | 13 +++---------- gui/default/syncthing/core/notifications.html | 10 +++++----- gui/default/syncthing/core/syncthingController.js | 8 ++++++++ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/gui/default/syncthing/core/notificationDirective.js b/gui/default/syncthing/core/notificationDirective.js index fc7810f6e..e3a4dd24c 100644 --- a/gui/default/syncthing/core/notificationDirective.js +++ b/gui/default/syncthing/core/notificationDirective.js @@ -2,20 +2,13 @@ angular.module('syncthing.core') .directive('notification', function () { return { restrict: 'E', - scope: true, + scope: {}, transclude: true, template: '
', link: function (scope, elm, attrs) { scope.visible = function () { - return scope.config.options.unackedNotificationIDs.indexOf(attrs.id) > -1; - } - scope.dismiss = function () { - var idx = scope.config.options.unackedNotificationIDs.indexOf(attrs.id); - if (idx > -1) { - scope.config.options.unackedNotificationIDs.splice(idx, 1); - scope.saveConfig(); - } - } + return scope.$parent.config.options.unackedNotificationIDs.indexOf(attrs.id) > -1; + }; } }; }); diff --git a/gui/default/syncthing/core/notifications.html b/gui/default/syncthing/core/notifications.html index 34b437a72..55987ab89 100644 --- a/gui/default/syncthing/core/notifications.html +++ b/gui/default/syncthing/core/notifications.html @@ -6,7 +6,7 @@

This is an example notification. ID of the notification should be appended to Options.UnackedNotificationIDs of the config.