ampersand-collection
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -38,2 +38,20 @@ var BackboneEvents = require('backbone-events-standalone'); | ||
// overridable serialize method | ||
serialize: function () { | ||
return this.map(function (model) { | ||
if (model.serialize) { | ||
return model.serialize(); | ||
} else { | ||
var out = {}; | ||
extend(out, model); | ||
delete out.collection; | ||
return out; | ||
} | ||
}); | ||
}, | ||
toJSON: function () { | ||
return this.serialize(); | ||
}, | ||
set: function (models, options) { | ||
@@ -293,4 +311,25 @@ options = extend({add: true, remove: true, merge: true}, options); | ||
var arrayMethods = [ | ||
'indexOf', | ||
'lastIndexOf', | ||
'every', | ||
'some', | ||
'forEach', | ||
'map', | ||
'filter', | ||
'reduce', | ||
'reduceRight' | ||
]; | ||
arrayMethods.forEach(function (method) { | ||
Collection.prototype[method] = function () { | ||
return this.models[method].apply(this.models, arguments); | ||
}; | ||
}); | ||
// alias each/forEach for maximum compatibility | ||
Collection.prototype.each = Collection.prototype.forEach; | ||
Collection.extend = classExtend; | ||
module.exports = Collection; |
{ | ||
"name": "ampersand-collection", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
@@ -5,0 +5,0 @@ "bugs": { |
@@ -197,1 +197,29 @@ var test = require('tape'); | ||
}); | ||
test('Proxy `Array.prototype` methods', function (t) { | ||
var c = new Collection(); | ||
c.set([{id: 'thing'}, {id: 'other'}]); | ||
var ids = c.map(function (item) { | ||
return item.id; | ||
}); | ||
t.deepEqual(ids, ['thing', 'other']); | ||
var count = 0; | ||
c.each(function () { | ||
count++; | ||
}); | ||
t.equal(count, 2); | ||
c.forEach(function () { | ||
count++; | ||
}); | ||
t.equal(count, 4); | ||
t.end(); | ||
}); | ||
test('Serialize/toJSON method', function (t) { | ||
var c = new Collection(); | ||
c.set([{id: 'thing'}, {id: 'other'}]); | ||
t.deepEqual([{id: 'thing'}, {id: 'other'}], c.serialize()); | ||
t.equal(JSON.stringify([{id: 'thing'}, {id: 'other'}]), JSON.stringify(c)); | ||
t.end(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22078
497