Skip to content

Instantly share code, notes, and snippets.

@ianwremmel
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianwremmel/9c87948063741c12fece to your computer and use it in GitHub Desktop.
Save ianwremmel/9c87948063741c12fece to your computer and use it in GitHub Desktop.
Collection object for a dirty-checking world.
// https://gist.github.com/ianwremmel/9c87948063741c12fece
/*
The MIT License (MIT)
Copyright (c) 2014 Ian W. Remmel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
'use strict';
var events = require('events');
var _ = require('lodash');
/**
* Generic Collection object inspired by Backbone.Collection and
* http://www.bennadel.com/blog/2292-extending-javascript-arrays-while-keeping-native-bracket-notation-functionality.htm
*/
var Collection = module.exports = function() {
// First, make sure the collection behaves like a native array
var collection = Object.create(Array.prototype);
// Then, apply the native array constructor.
collection = Array.apply(collection, arguments) || collection;
// Make collection an event emitter;
Collection.addEventSupport(collection);
// Duckpunch array methods
Collection.wrapNativeMethods(collection);
// Inject Lo-Dash methods
Collection.injectLodashMethods(collection);
return collection;
};
// Can't use the standard util.inherits technique because we're operating on
// an instance.
Collection.addEventSupport = function(collection) {
window.ee = events.EventEmitter;
for (var key in events.EventEmitter.prototype) {
if (_.isFunction(events.EventEmitter.prototype[key])) {
collection[key] = events.EventEmitter.prototype[key].bind(collection);
}
}
// _.extend(collection.prototype, events.EventEmitter.prototype);
events.EventEmitter.apply(collection);
};
// Some dirty-checking frameworks only allow watching data under certain
// conditions, so add some event support to the array mutator methods.
Collection.wrapNativeMethods = function(collection) {
var methods = {
fill: 'add',
pop: 'remove',
push: 'add',
reverse: 'sort',
shift: 'remove',
sort: 'sort',
unshift: 'add'
};
_.forEach(methods, function(eventName, methodName) {
collection[methodName] = _.wrap(collection[methodName], function(func) {
var args = Array.prototype.slice.call(arguments);
args.shift();
var ret = func.apply(this, args);
this.emit(eventName, this);
return ret;
});
});
// splice can't be wrapped automatically because it may add and remove items.
collection.splice = _.wrap(collection.splice, function(func, index, howMany) {
var args = Array.prototype.slice.call(arguments);
args.shift();
var ret = func.apply(this, args);
if (howMany > 0) {
this.emit('remove', this);
}
if (arguments.length > 2) {
this.emit('add', this);
}
return ret;
}).bind(collection);
};
Collection.injectLodashMethods = function(collection) {
// Import methods from Lo-Dash.
var methods = [
'forEach', 'each', 'map', 'collect', 'reduce', 'foldl', 'inject',
'reduceRight', 'foldr', 'find', 'detect', 'filter', 'select', 'reject',
'every', 'all', 'some', 'any', 'include', 'contains', 'invoke', 'max',
'min', 'toArray', 'size', 'first', 'head', 'take', 'initial', 'rest',
'tail', 'drop', 'last', 'without', 'difference', 'indexOf', 'shuffle',
'lastIndexOf', 'isEmpty', 'chain', 'sample', 'where', 'findWhere', 'pluck'
];
methods.forEach(function(methodName) {
// Don't inject methods that already exist
if (!collection[methodName]) {
collection[methodName] = function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(this.items);
return _[methodName].apply(_, args);
}.bind(collection);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment