Skip to content

Commit aa86a52

Browse files
adding emitting for add/remove
1 parent 13be84d commit aa86a52

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

component.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"version": "0.0.1",
66
"keywords": ["enumerable", "data", "model", "db"],
77
"dependencies": {
8+
"component/emitter": "*",
89
"component/enumerable": "*"
910
},
1011
"development": {},

index.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* Module dependencies.
44
*/
55

6-
var Enumerable = require('enumerable');
6+
var Emitter = require('emitter')
7+
, Enumerable = require('enumerable');
78

89
/**
910
* Expose `Collection`.
@@ -22,6 +23,12 @@ function Collection(models) {
2223
this.models = models || [];
2324
}
2425

26+
/**
27+
* Mixin emitter.
28+
*/
29+
30+
Emitter(Collection.prototype);
31+
2532
/**
2633
* Mixin enumerable.
2734
*/
@@ -61,7 +68,9 @@ Collection.prototype.length = function(){
6168

6269
Collection.prototype.add =
6370
Collection.prototype.push = function(model){
64-
return this.models.push(model);
71+
var length = this.models.push(model);
72+
this.emit('add', model);
73+
return length;
6574
};
6675

6776
/**
@@ -73,6 +82,9 @@ Collection.prototype.push = function(model){
7382

7483
Collection.prototype.remove = function(model){
7584
var i = this.indexOf(model);
76-
if (~i) this.models.splice(i, 1);
85+
if (~i) {
86+
this.models.splice(i, 1);
87+
this.emit('remove', model);
88+
}
7789
return !! ~i;
7890
};

0 commit comments

Comments
 (0)