|
1 | 1 | /**! |
2 | | - * @license angular-flash v0.1.8 |
| 2 | + * @license angular-flash v0.1.9 |
3 | 3 | * Copyright (c) 2013 William L. Bunselmeyer. https://github.com/wmluke/angular-flash |
4 | 4 | * License: MIT |
5 | 5 | */ |
|
10 | 10 |
|
11 | 11 | var Flash = function (options) { |
12 | 12 | var _options = angular.extend({ |
| 13 | + id: null, |
| 14 | + subscribers: [], |
13 | 15 | classnames: { |
14 | 16 | error: [], |
15 | 17 | warn: [], |
|
19 | 21 | }, options); |
20 | 22 |
|
21 | 23 | var _self = this; |
22 | | - var _subscribers = []; |
23 | 24 | var _success; |
24 | 25 | var _info; |
25 | 26 | var _warn; |
26 | 27 | var _error; |
27 | 28 | var _type; |
28 | 29 |
|
29 | 30 | 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) { |
32 | 35 | subscriber.cb(message, type); |
33 | 36 | } |
34 | 37 | }); |
35 | 38 | } |
36 | 39 |
|
37 | 40 | this.clean = function () { |
38 | | - _subscribers = []; |
| 41 | + _options.subscribers = []; |
39 | 42 | _success = null; |
40 | 43 | _info = null; |
41 | 44 | _warn = null; |
42 | 45 | _error = null; |
43 | 46 | _type = null; |
44 | 47 | }; |
45 | 48 |
|
46 | | - this.subscribe = function (subscriber, type) { |
47 | | - _subscribers.push({ |
| 49 | + this.subscribe = function (subscriber, type, id) { |
| 50 | + _options.subscribers.push({ |
48 | 51 | cb: subscriber, |
49 | | - type: type |
| 52 | + type: type, |
| 53 | + id: id |
50 | 54 | }); |
51 | 55 | }; |
52 | 56 |
|
| 57 | + this.to = function (id) { |
| 58 | + var options = angular.copy(_options); |
| 59 | + options.id = id; |
| 60 | + return new Flash(options); |
| 61 | + }; |
| 62 | + |
53 | 63 | Object.defineProperty(this, 'success', { |
54 | 64 | get: function () { |
55 | 65 | return _success; |
|
111 | 121 | return _options.classnames; |
112 | 122 | } |
113 | 123 | }); |
| 124 | + |
| 125 | + Object.defineProperty(this, 'id', { |
| 126 | + get: function () { |
| 127 | + return _options.id; |
| 128 | + } |
| 129 | + }); |
114 | 130 | }; |
115 | 131 |
|
116 | 132 | angular.module('angular-flash.service', []) |
|
196 | 212 | } |
197 | 213 | } |
198 | 214 |
|
199 | | - flash.subscribe(show, attr.flashAlert); |
| 215 | + flash.subscribe(show, attr.flashAlert, attr.id); |
200 | 216 |
|
201 | 217 | /** |
202 | 218 | * Fixes timing issues: display the last flash message sent before this directive subscribed. |
|
0 commit comments