Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added add/remove methods with emitting #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"version": "0.0.1",
"keywords": ["enumerable", "data", "model", "db"],
"dependencies": {
"component/emitter": "*",
"component/enumerable": "*"
},
"development": {},
Expand Down
31 changes: 29 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* Module dependencies.
*/

var Enumerable = require('enumerable');
var Emitter = require('emitter')
, Enumerable = require('enumerable');

/**
* Expose `Collection`.
Expand All @@ -22,6 +23,12 @@ function Collection(models) {
this.models = models || [];
}

/**
* Mixin emitter.
*/

Emitter(Collection.prototype);

/**
* Mixin enumerable.
*/
Expand Down Expand Up @@ -59,6 +66,26 @@ Collection.prototype.length = function(){
* @api public
*/

Collection.prototype.add =
Collection.prototype.push = function(model){
return this.models.push(model);
var length = this.models.push(model);
this.emit('add', model);
return length;
};

/**
* Remove `model` from the collection, returning `true` when present,
* otherwise `false`.
*
* @param {Object} model
* @api public
*/

Collection.prototype.remove = function(model){
var i = this.indexOf(model);
if (~i) {
this.models.splice(i, 1);
this.emit('remove', model);
}
return !! ~i;
};