Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

refactor(alert): move dismiss-on-timeout to an attribute #4405

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions src/alert/alert.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
angular.module('ui.bootstrap.alert', [])

.controller('AlertController', ['$scope', '$attrs', function($scope, $attrs) {
.controller('AlertController', ['$scope', '$attrs', '$timeout', function($scope, $attrs, $timeout) {
$scope.closeable = !!$attrs.close;
this.close = $scope.close;

if (angular.isDefined($attrs.dismissOnTimeout)) {
$timeout(function() {
$scope.close();
}, parseInt($attrs.dismissOnTimeout, 10));
}
}])

.directive('alert', function() {
Expand All @@ -19,15 +24,4 @@ angular.module('ui.bootstrap.alert', [])
close: '&'
}
};
})

.directive('dismissOnTimeout', ['$timeout', function($timeout) {
return {
require: 'alert',
link: function(scope, element, attrs, alertCtrl) {
$timeout(function() {
alertCtrl.close();
}, parseInt(attrs.dismissOnTimeout, 10));
}
};
}]);
});
14 changes: 11 additions & 3 deletions src/alert/test/alert.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
describe('alert', function() {
var scope, $compile, $templateCache;
var element;
var element, scope, $compile, $templateCache, $timeout;

beforeEach(module('ui.bootstrap.alert'));
beforeEach(module('template/alert/alert.html'));

beforeEach(inject(function($rootScope, _$compile_, _$templateCache_) {
beforeEach(inject(function($rootScope, _$compile_, _$templateCache_, _$timeout_) {
scope = $rootScope;
$compile = _$compile_;
$templateCache = _$templateCache_;
$timeout = _$timeout_;

element = angular.element(
'<div>' +
Expand Down Expand Up @@ -128,4 +128,12 @@ describe('alert', function() {
expect(element).toHaveClass('alert-info');
});

it('should close automatically if dismiss-on-timeout is defined on the element', function() {
scope.removeAlert = jasmine.createSpy();
$compile('<alert close="removeAlert()" dismiss-on-timeout="500">Default alert!</alert>')(scope);
scope.$digest();

$timeout.flush();
expect(scope.removeAlert).toHaveBeenCalled();
});
});
20 changes: 0 additions & 20 deletions src/alert/test/dismissOnTimeout.spec.js

This file was deleted.