Skip to content

Commit b1eefcd

Browse files
committed
Convert notification directive and service to not use scope
1 parent 5ac0ac1 commit b1eefcd

File tree

3 files changed

+38
-47
lines changed

3 files changed

+38
-47
lines changed

src/notification/notification-list.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<div data-ng-show="notifications.data.length > 0">
2-
<div ng-repeat="notification in notifications.data">
1+
<div data-ng-show="$ctrl.notifications.data.length > 0">
2+
<div ng-repeat="notification in $ctrl.notifications.data">
33
<pf-inline-notification pf-notification-type="notification.type"
44
pf-notification-header="notification.header"
55
pf-notification-message="notification.message"

src/notification/notification.js

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ angular.module('patternfly.notification').provider('Notifications', function ()
103103
this.delay = 8000;
104104
this.verbose = true;
105105
this.notifications = {};
106+
this.notifications.data = [];
106107
this.persist = {'error': true, 'httpError': true};
107108

108109
this.setDelay = function (delay) {
@@ -119,7 +120,7 @@ angular.module('patternfly.notification').provider('Notifications', function ()
119120
this.persist = persist;
120121
};
121122

122-
this.$get = ['$rootScope', '$timeout', '$log', function ($rootScope, $timeout, $log) {
123+
this.$get = ['$timeout', '$log', function ($timeout, $log) {
123124
var delay = this.delay;
124125
var notifications = this.notifications;
125126
var verbose = this.verbose;
@@ -132,15 +133,8 @@ angular.module('patternfly.notification').provider('Notifications', function ()
132133
warn: { type: 'warning', header: 'Warning!', log: 'warn'}
133134
};
134135

135-
$rootScope.notifications = {};
136-
$rootScope.notifications.data = [];
137-
138-
$rootScope.notifications.remove = function (index) {
139-
$rootScope.notifications.data.splice(index, 1);
140-
};
141-
142-
if (!$rootScope.notifications) {
143-
$rootScope.notifications.data = [];
136+
if (!notifications) {
137+
notifications.data = [];
144138
}
145139

146140
notifications.message = function (type, header, message, isPersistent, closeCallback, actionTitle, actionCallback, menuActions) {
@@ -156,7 +150,7 @@ angular.module('patternfly.notification').provider('Notifications', function ()
156150
};
157151

158152
notification.show = true;
159-
$rootScope.notifications.data.push(notification);
153+
notifications.data.push(notification);
160154

161155
if (!notification.isPersistent) {
162156
notification.viewing = false;
@@ -198,14 +192,15 @@ angular.module('patternfly.notification').provider('Notifications', function ()
198192
};
199193

200194
notifications.remove = function (notification) {
201-
var index = $rootScope.notifications.data.indexOf(notification);
195+
var index = notifications.data.indexOf(notification);
202196
if (index !== -1) {
203197
notifications.removeIndex(index);
204198
}
205199
};
206200

207201
notifications.removeIndex = function (index) {
208-
$rootScope.notifications.remove(index);
202+
//notifications.remove(index);
203+
notifications.data.splice(index, 1);
209204
};
210205

211206
notifications.setViewing = function (notification, viewing) {
@@ -215,20 +210,18 @@ angular.module('patternfly.notification').provider('Notifications', function ()
215210
}
216211
};
217212

218-
notifications.data = $rootScope.notifications.data;
219-
220213
return notifications;
221214
}];
222215

223216
});
224217

225218
/**
226219
* @ngdoc directive
227-
* @name patternfly.notification.directive:pfNotificationList
220+
* @name patternfly.notification.component:pfNotificationList
228221
* @restrict E
229222
*
230223
* @description
231-
* Using this directive automatically creates a list of notifications generated by the {@link api/patternfly.notification.Notification notification} service.
224+
* Using this component automatically creates a list of notifications generated by the {@link api/patternfly.notification.Notification notification} service.
232225
*
233226
* @example
234227
<example module="patternfly.notification">
@@ -298,16 +291,14 @@ angular.module('patternfly.notification').provider('Notifications', function ()
298291
299292
</example>
300293
*/
301-
angular.module('patternfly.notification').directive('pfNotificationList', function () {
302-
'use strict';
303-
304-
return {
305-
restrict: 'E',
306-
controller: NotificationListController,
307-
templateUrl: 'notification/notification-list.html'
308-
};
309-
310-
function NotificationListController ($scope, $rootScope) {
311-
$scope.notifications = $rootScope.notifications;
294+
angular.module('patternfly.notification').component('pfNotificationList', {
295+
templateUrl: 'notification/notification-list.html',
296+
controller: function (Notifications) {
297+
'use strict';
298+
var ctrl = this;
299+
300+
ctrl.$onInit = function () {
301+
ctrl.notifications = Notifications;
302+
};
312303
}
313304
});

test/notification/notification.spec.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,42 @@ describe('pf-notification', function () {
1919

2020
it('should propagate to the $rootScope correctly', function () {
2121

22-
expect($scope.notifications.data.length).toBe(0);
22+
expect(Notifications.data.length).toBe(0);
2323

2424
Notifications.info('aloha');
2525

26-
expect($scope.notifications.data.length).toBe(1);
27-
expect($scope.notifications.data[0].type).toBe('info');
28-
expect($scope.notifications.data[0].message).toBe('aloha');
26+
expect(Notifications.data.length).toBe(1);
27+
expect(Notifications.data[0].type).toBe('info');
28+
expect(Notifications.data[0].message).toBe('aloha');
2929

3030
Notifications.warn('achtung');
3131

32-
expect($scope.notifications.data.length).toBe(2);
33-
expect($scope.notifications.data[1].type).toBe('warning');
34-
expect($scope.notifications.data[1].message).toBe('achtung');
32+
expect(Notifications.data.length).toBe(2);
33+
expect(Notifications.data[1].type).toBe('warning');
34+
expect(Notifications.data[1].message).toBe('achtung');
3535

3636
Notifications.success('allright');
3737

38-
expect($scope.notifications.data.length).toBe(3);
39-
expect($scope.notifications.data[2].type).toBe('success');
40-
expect($scope.notifications.data[2].message).toBe('allright');
38+
expect(Notifications.data.length).toBe(3);
39+
expect(Notifications.data[2].type).toBe('success');
40+
expect(Notifications.data[2].message).toBe('allright');
4141

4242
Notifications.error('noway');
4343

44-
expect($scope.notifications.data.length).toBe(4);
45-
expect($scope.notifications.data[3].type).toBe('danger');
46-
expect($scope.notifications.data[3].message).toBe('noway');
44+
expect(Notifications.data.length).toBe(4);
45+
expect(Notifications.data[3].type).toBe('danger');
46+
expect(Notifications.data[3].message).toBe('noway');
4747

4848
var httpResponse = {data:{message: 'http'}};
4949

5050
Notifications.httpError('error', httpResponse);
5151

52-
expect($scope.notifications.data.length).toBe(5);
53-
expect($scope.notifications.data[4].type).toBe('danger');
54-
expect($scope.notifications.data[4].message).toBe('error (http)');
52+
expect(Notifications.data.length).toBe(5);
53+
expect(Notifications.data[4].type).toBe('danger');
54+
expect(Notifications.data[4].message).toBe('error (http)');
5555

5656
$timeout.flush();
57-
expect($scope.notifications.data.length).toBe(2);
57+
expect(Notifications.data.length).toBe(2);
5858
});
5959
});
6060

0 commit comments

Comments
 (0)