Skip to content
Closed
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
9 changes: 6 additions & 3 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
"version": "0.0.1",
"keywords": ["enumerable", "data", "model", "db"],
"dependencies": {
"component/enumerable": "*"
"component/enumerable": "*",
"component/emitter": "*"
},
"development": {
"component/model": "*"
},
"development": {},
"license": "MIT",
"scripts": [
"index.js"
]
}
}
126 changes: 120 additions & 6 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 Enumerable = require('enumerable'),
Emitter = require('emitter');

/**
* Expose `Collection`.
Expand All @@ -28,6 +29,12 @@ function Collection(models) {

Enumerable(Collection.prototype);

/**
* Mixin emitter
*/

Emitter(Collection.prototype);

/**
* Iterator implementation.
*/
Expand All @@ -52,13 +59,120 @@ Collection.prototype.length = function(){
};

/**
* Add `model` to the collection and return the index.
* Removes the last element from an array and returns that element
*
* @return {Mixed} removed element
*/

Collection.prototype.pop = function() {
var models = this.models,
ret = models.pop.apply(models, arguments);
this.emit('pop', ret);
this.emit('remove', ret);
return ret;
};

/**
* Push a value onto the end of the array,
* returning the length of the array
*
* @param {Object} model
* @param {Mixed, ...} elements
* @return {Number}
* @api public
*/

Collection.prototype.push = function(model){
return this.models.push(model);
Collection.prototype.push = function() {
var models = this.models,
ret = models.push.apply(models, arguments),
args = [].slice.call(arguments);
this.emit('push', ret);
for(var i = 0, len = args.length; i < len; i++) this.emit('add', args[i]);
return ret;
};

/**
* Reverses an array in place.
*
* @return {Array}
*/

Collection.prototype.reverse = function() {
var models = this.models,
ret = models.reverse.apply(models, arguments);
this.emit('reverse', ret);
return ret;
};

/**
* Removes the first element from an array and returns that element.
*
* @return {Mixed}
*/

Collection.prototype.shift = function() {
var models = this.models,
ret = this.models.shift.apply(this, arguments);
this.emit('shift', ret);
this.emit('remove', ret);
return ret;
};

/**
* Sorts the elements of an array.
*
* @return {Array}
*/

Collection.prototype.sort = function() {
var models = this.models,
ret = models.sort.apply(models, arguments);
this.emit('sort', ret);
return ret;
};

/**
* Adds and/or removes elements from an array.
*
* @param {Number} index
* @param {Number} howMany
* @param {Mixed, ...} elements
* @return {Array} removed elements
*/

Collection.prototype.splice = function() {
var models = this.models,
ret = models.splice.apply(models, arguments),
added = [].slice.call(arguments, 2);
this.emit('splice', ret);
for(var i = 0, len = ret.length; i < len; i++) this.emit('remove', ret[i]);
for( i = 0, len = added.length; i < len; i++) this.emit('add', added[i]);
return ret;
};

/**
* Adds one or more elements to the front of an array
* and returns the new length of the array.
*
* @param {Mixed, ...} elements
* @return {Number} length
*/

Collection.prototype.unshift = function() {
var models = this.models,
ret = models.unshift.apply(models, arguments),
args = [].slice.call(arguments);
this.emit('unshift', ret);
for(var i = 0, len = args.length; i < len; i++) this.emit('add', args[i]);
return ret;
};

/**
* Clear a list
*
* @return {Collection}
*/

Collection.prototype.clear = function() {
this.models = [];
this.emit('clear');
return this;
};
28 changes: 28 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<html>
<head>
<title>collection component</title>
<link rel="stylesheet" href="../build/build.css">
</head>
<body>
<h1>collection component</h1>
<script src="../build/build.js" type="text/javascript"></script>
<script type="text/javascript">
var Collection = require('collection'),
model = require('component-model/lib'),
User = model('User')
.attr('name')
.attr('age');

var matt = new User().name('matt').age(23),
jim = new User().name('jim').age(42);

var users = new Collection;

users.on('add', function(model) {
console.log(model);
});

users.push(jim);
</script>
</body>
</html>