From 9671dd55dd083c48ab25989c183adf9af877c199 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Sun, 2 Jun 2013 20:59:28 -0700 Subject: [PATCH 1/4] added an `add` alias --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 96f8077..208d197 100644 --- a/index.js +++ b/index.js @@ -59,6 +59,7 @@ Collection.prototype.length = function(){ * @api public */ +Collection.prototype.add = Collection.prototype.push = function(model){ return this.models.push(model); }; From 13be84dc5557d2901e6eb268964c6bddedc89a49 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Sun, 2 Jun 2013 21:06:35 -0700 Subject: [PATCH 2/4] added remove method --- index.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/index.js b/index.js index 208d197..b670ebd 100644 --- a/index.js +++ b/index.js @@ -63,3 +63,16 @@ Collection.prototype.add = Collection.prototype.push = function(model){ return this.models.push(model); }; + +/** + * Remove `model` from the collection. + * + * @param {Object} model + * @api public + */ + +Collection.prototype.remove = function(model){ + var i = this.indexOf(model); + if (~i) this.models.splice(i, 1); + return !! ~i; +}; From aa86a52f3807b4fb96f9a214ba4eee15b322a9e9 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Sun, 2 Jun 2013 21:10:12 -0700 Subject: [PATCH 3/4] adding emitting for add/remove --- component.json | 1 + index.js | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/component.json b/component.json index 1aee2ff..3aac387 100644 --- a/component.json +++ b/component.json @@ -5,6 +5,7 @@ "version": "0.0.1", "keywords": ["enumerable", "data", "model", "db"], "dependencies": { + "component/emitter": "*", "component/enumerable": "*" }, "development": {}, diff --git a/index.js b/index.js index b670ebd..ae4be3d 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,8 @@ * Module dependencies. */ -var Enumerable = require('enumerable'); +var Emitter = require('emitter') + , Enumerable = require('enumerable'); /** * Expose `Collection`. @@ -22,6 +23,12 @@ function Collection(models) { this.models = models || []; } +/** + * Mixin emitter. + */ + +Emitter(Collection.prototype); + /** * Mixin enumerable. */ @@ -61,7 +68,9 @@ Collection.prototype.length = function(){ 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; }; /** @@ -73,6 +82,9 @@ Collection.prototype.push = function(model){ Collection.prototype.remove = function(model){ var i = this.indexOf(model); - if (~i) this.models.splice(i, 1); + if (~i) { + this.models.splice(i, 1); + this.emit('remove', model); + } return !! ~i; }; From c45282a071a7ace0817a2e2a31c428e672b54ccc Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Sun, 2 Jun 2013 21:11:48 -0700 Subject: [PATCH 4/4] added a better comment for the remove method --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ae4be3d..60cf5a6 100644 --- a/index.js +++ b/index.js @@ -74,7 +74,8 @@ Collection.prototype.push = function(model){ }; /** - * Remove `model` from the collection. + * Remove `model` from the collection, returning `true` when present, + * otherwise `false`. * * @param {Object} model * @api public