-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-observe.js
50 lines (44 loc) · 1.31 KB
/
ng-observe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"use strict";
angular.module('ngObserve', [])
.factory('Observe', function () {
var Observe = function (data) {
var _listeners = [];
var _data = data;
var _id = 1;
var Subscriber = function (id, cb) {
this.id = id;
this.cb = cb;
};
Subscriber.prototype = {
cancel: function () {
for (var i = 0; i < _listeners.length; i++) {
if (this.id === _listeners[i].id) {
_listeners.splice(i, 1);
return true;
}
}
}
};
this.subscribe = function (cb) {
try {
if (typeof cb === "function") {
var sbcr = new Subscriber(_id++, cb);
_listeners.push(sbcr);
sbcr.cb(_data);
return sbcr;
} else {
throw new TypeError('Argument is not a function!');
}
} catch (err) {
console.error(err);
}
};
this.next = function (data) {
_data = data;
for (var i = 0; i < _listeners.length; i++) {
_listeners[i].cb(_data);
}
};
};
return Observe;
});