Skip to content

Commit 615ad89

Browse files
committed
Added support to send flashes only to named subscribers.
1 parent 2f4645d commit 615ad89

File tree

12 files changed

+173
-30
lines changed

12 files changed

+173
-30
lines changed

Gruntfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ module.exports = function (grunt) {
7070
browsers: ['PhantomJS']
7171
},
7272
debug: {
73-
singleRun: false
73+
singleRun: false,
74+
reporters: ['progress', 'junit']
7475
}
7576
},
7677
concat: {

app/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,23 @@
5252
</div>
5353

5454
<div ng-view></div>
55+
56+
<div class="row row-alert">
57+
<div class="span12">
58+
<div id="alert-1" flash-alert active-class="in" class="alert fade" duration="0">
59+
<button type="button" class="close" ng-click="hide()">&times;</button>
60+
<i class="icon-ok-sign"></i>
61+
<strong class="alert-heading">{{flash.type}}</strong>
62+
<span class="alert-message">{{flash.message}}</span>
63+
<h4 class="pull-right">Alert #1</h4>
64+
</div>
65+
66+
</div>
67+
</div>
5568
</div>
5669

5770
<script src="components/jquery/jquery.js"></script>
71+
<script src="components/bootstrap/docs/assets/js/bootstrap.min.js"></script>
5872
<script src="components/angular/angular.js"></script>
5973
<script src="components/angular-route/angular-route.js"></script>
6074
<script src="angular-flash.js"></script>

app/scripts/controllers/main-ctrl.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(function () {
22
'use strict';
33

4-
var MainCtrl = function ($scope, flash, $timeout) {
4+
var MainCtrl = function ($scope, flash) {
5+
56

67
$scope.all = function () {
78
$scope.info();
@@ -14,21 +15,37 @@
1415
flash.info = 'info message';
1516
};
1617

18+
$scope.info1 = function () {
19+
flash.to('alert-1').info = 'info message';
20+
};
21+
1722
$scope.warn = function () {
1823
flash.warn = 'warn message';
1924
};
2025

26+
$scope.warn1 = function () {
27+
flash.to('alert-1').warn = 'warn message';
28+
};
29+
2130
$scope.success = function () {
2231
flash.success = 'success message';
2332
};
2433

34+
$scope.success1 = function () {
35+
flash.to('alert-1').success = 'success message';
36+
};
37+
2538
$scope.error = function () {
2639
flash.error = 'error message';
2740
};
2841

42+
$scope.error1 = function () {
43+
flash.to('alert-1').error = 'error message';
44+
};
45+
2946
$scope.all();
3047
};
3148

3249
angular.module('App.main-ctrl', [])
33-
.controller('MainCtrl', ['$scope', 'flash', '$timeout', MainCtrl]);
34-
}());
50+
.controller('MainCtrl', ['$scope', 'flash', MainCtrl]);
51+
}());

app/views/main.html

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,27 @@
22
<div class="navbar-inner">
33
<div class="container">
44
<a class="brand" href="/">angular-flash <i class="icon-bolt"></i></a>
5-
<button class="btn btn-info" ng-click="info()"><i class="icon-info-sign"></i> Info</button>
6-
<button class="btn btn-warning" ng-click="warn()"><i class="icon-warning-sign"></i> Warning</button>
7-
<button class="btn btn-success" ng-click="success()"><i class="icon-ok-sign"></i> Success</button>
8-
<button class="btn btn-danger" ng-click="error()"><i class="icon-exclamation-sign"></i> Error</button>
5+
6+
<div class="btn-group">
7+
<button class="btn btn-info" ng-click="info()"><i class="icon-info-sign"></i> Info</button>
8+
<button class="btn btn-info" ng-click="info1()">Alert #1</button>
9+
</div>
10+
11+
<div class="btn-group">
12+
<button class="btn btn-warning" ng-click="warn()"><i class="icon-warning-sign"></i> Warning</button>
13+
<button class="btn btn-warning" ng-click="warn1()">Alert #1</button>
14+
</div>
15+
16+
<div class="btn-group">
17+
<button class="btn btn-success" ng-click="success()"><i class="icon-ok-sign"></i> Success</button>
18+
<button class="btn btn-success" ng-click="success1()">Alert #1</button>
19+
</div>
20+
21+
<div class="btn-group">
22+
<button class="btn btn-danger" ng-click="error()"><i class="icon-exclamation-sign"></i> Error</button>
23+
<button class="btn btn-danger" ng-click="error1()">Alert #1</button>
24+
</div>
25+
926
<button class="btn btn-primary" ng-click="all()"><i class="icon-flag"></i> All</button>
1027
</div>
1128
</div>

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-flash",
33
"description": "Flash messages for Angular Js",
4-
"version": "0.1.8",
4+
"version": "0.1.9",
55
"main": [
66
"dist/angular-flash.min.js",
77
"dist/angular-flash.js"

dist/angular-flash.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**!
2-
* @license angular-flash v0.1.8
2+
* @license angular-flash v0.1.9
33
* Copyright (c) 2013 William L. Bunselmeyer. https://github.com/wmluke/angular-flash
44
* License: MIT
55
*/
@@ -10,6 +10,8 @@
1010

1111
var Flash = function (options) {
1212
var _options = angular.extend({
13+
id: null,
14+
subscribers: [],
1315
classnames: {
1416
error: [],
1517
warn: [],
@@ -19,37 +21,45 @@
1921
}, options);
2022

2123
var _self = this;
22-
var _subscribers = [];
2324
var _success;
2425
var _info;
2526
var _warn;
2627
var _error;
2728
var _type;
2829

2930
function _notify(type, message) {
30-
angular.forEach(_subscribers, function (subscriber) {
31-
if (!subscriber.type || subscriber.type === type) {
31+
angular.forEach(_options.subscribers, function (subscriber) {
32+
var matchesType = !subscriber.type || subscriber.type === type;
33+
var matchesId = (!_options.id && !subscriber.id) || subscriber.id === _options.id;
34+
if (matchesType && matchesId) {
3235
subscriber.cb(message, type);
3336
}
3437
});
3538
}
3639

3740
this.clean = function () {
38-
_subscribers = [];
41+
_options.subscribers = [];
3942
_success = null;
4043
_info = null;
4144
_warn = null;
4245
_error = null;
4346
_type = null;
4447
};
4548

46-
this.subscribe = function (subscriber, type) {
47-
_subscribers.push({
49+
this.subscribe = function (subscriber, type, id) {
50+
_options.subscribers.push({
4851
cb: subscriber,
49-
type: type
52+
type: type,
53+
id: id
5054
});
5155
};
5256

57+
this.to = function (id) {
58+
var options = angular.copy(_options);
59+
options.id = id;
60+
return new Flash(options);
61+
};
62+
5363
Object.defineProperty(this, 'success', {
5464
get: function () {
5565
return _success;
@@ -111,6 +121,12 @@
111121
return _options.classnames;
112122
}
113123
});
124+
125+
Object.defineProperty(this, 'id', {
126+
get: function () {
127+
return _options.id;
128+
}
129+
});
114130
};
115131

116132
angular.module('angular-flash.service', [])
@@ -196,7 +212,7 @@
196212
}
197213
}
198214

199-
flash.subscribe(show, attr.flashAlert);
215+
flash.subscribe(show, attr.flashAlert, attr.id);
200216

201217
/**
202218
* Fixes timing issues: display the last flash message sent before this directive subscribed.

dist/angular-flash.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "angular-flash",
33
"description": "Flash messages for Angular JS",
44
"private": false,
5-
"version": "0.1.8",
5+
"version": "0.1.9",
66
"homepage": "https://github.com/wmluke/angular-flash",
77
"author": {
88
"name": "William L. Bunselmeyer",

readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ var FooController = function(flash){
5151

5252
// Publish a error flash
5353
flash.error = 'Fail!';
54+
55+
// Publish an info flash to the `alert-1` subscriber
56+
flash.to('alert-1').info = 'Only for alert 1';
5457
};
5558

5659
FooController.$inject = ['flash'];
@@ -78,6 +81,12 @@ Use the `flash-alert` directive to subscribe to flash messages...
7881
<span class="alert-message">{{flash.message}}</span>
7982
</div>
8083

84+
<!-- Subscribe to all flash messages sent to `alert-1`. -->
85+
<div id="alert-1" flash-alert active-class="in" class="alert fade">
86+
<strong class="alert-heading">Boo!</strong>
87+
<span class="alert-message">{{flash.message}}</span>
88+
</div>
89+
8190
<!-- Set the display duration in milli-secs. Default is 5000, 0 disables the fade-away. -->
8291
<div flash-alert active-class="in" class="alert fade" duration="0">
8392
<!-- Manually hide the alert with `hide()` -->

src/directives/flash-alert-directive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
}
6060
}
6161

62-
flash.subscribe(show, attr.flashAlert);
62+
flash.subscribe(show, attr.flashAlert, attr.id);
6363

6464
/**
6565
* Fixes timing issues: display the last flash message sent before this directive subscribed.

0 commit comments

Comments
 (0)