Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ampersand-collection

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ampersand-collection - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

.jshintignore

69

ampersand-collection.js
var BackboneEvents = require('backbone-events-standalone');
var classExtend = require('./classExtend');
var classExtend = require('ampersand-class-extend');
var isArray = require('is-array');

@@ -43,3 +43,3 @@ var extend = require('extend-object');

models = singular ? (models ? [models] : []) : models.slice();
var id, model, attrs, existing, sort;
var id, model, attrs, existing, sort, i, length;
var at = options.at;

@@ -53,6 +53,5 @@ var sortable = this.comparator && (at == null) && options.sort !== false;

// Turn bare objects into model references, and prevent invalid models
// from being added.
for (var i = 0, length = models.length; i < length; i++) {
for (i = 0, length = models.length; i < length; i++) {
attrs = models[i] || {};

@@ -96,3 +95,3 @@ if (this.isModel(attrs)) {

if (remove) {
for (var i = 0, length = this.length; i < length; i++) {
for (i = 0, length = this.length; i < length; i++) {
if (!modelMap[(model = this.models[i]).cid]) toRemove.push(model);

@@ -108,3 +107,3 @@ }

if (at != null) {
for (var i = 0, length = toAdd.length; i < length; i++) {
for (i = 0, length = toAdd.length; i < length; i++) {
this.models.splice(at + i, 0, toAdd[i]);

@@ -115,3 +114,3 @@ }

var orderedModels = order || toAdd;
for (var i = 0, length = orderedModels.length; i < length; i++) {
for (i = 0, length = orderedModels.length; i < length; i++) {
this.models.push(orderedModels[i]);

@@ -127,4 +126,5 @@ }

if (!options.silent) {
for (var i = 0, length = toAdd.length; i < length; i++) {
//(model = toAdd[i]).trigger('add', model, this, options);
for (i = 0, length = toAdd.length; i < length; i++) {
model = toAdd[i];
if (model.trigger) model.trigger('add', model, this, options);
}

@@ -145,7 +145,7 @@ if (sort || (order && order.length)) this.trigger('sort', this, options);

// Get the model at the given index.
Collection.prototype.at = function(index) {
Collection.prototype.at = function (index) {
return this.models[index];
};
Collection.prototype.remove = function (models) {
Collection.prototype.remove = function (models, options) {
var singular = !isArray(models);

@@ -160,11 +160,11 @@ var i, length, model, index;

this._deIndex(model);
index = this.indexOf(model);
index = this.models.indexOf(model);
this.models.splice(index, 1);
if (!options.silent) {
options.index = index;
model.trigger('remove', model, this, options);
options.index = index;
if (model.trigger) model.trigger('remove', model, this, options);
}
this._removeReference(model, options);
}
return singular ? models[0] : models;
}
return singular ? models[0] : models;
};

@@ -176,3 +176,3 @@

// Useful for bulk operations and optimizations.
Collection.prototype.reset = function(models, options) {
Collection.prototype.reset = function (models, options) {
options || (options = {});

@@ -192,3 +192,3 @@ for (var i = 0, length = this.models.length; i < length; i++) {

// is first initialized or reset.
Collection.prototype._reset = function() {
Collection.prototype._reset = function () {
var list = this.indexes || [];

@@ -205,3 +205,3 @@ var i = 0;

Collection.prototype._prepareModel = function(attrs, options) {
Collection.prototype._prepareModel = function (attrs, options) {
// if we haven't defined a constructor, skip this

@@ -215,3 +215,3 @@ if (!this.model) return attrs;

options.collection = this;
return new this.model(attrs, options)
return new this.model(attrs, options);
}

@@ -221,3 +221,3 @@ };

Collection.prototype._deIndex = function (model) {
for (name in this._indexes) {
for (var name in this._indexes) {
delete this._indexes[name][model[name] || model.get(name)];

@@ -227,4 +227,3 @@ }

// Internal method to create a model's ties to a collection.
Collection.prototype._addReference = function(model, options) {
Collection.prototype._index = function (model) {
for (var name in this._indexes) {

@@ -234,12 +233,28 @@ var indexVal = model[name] || (model.get && model.get(name));

}
//if (!model.collection) model.collection = this;
//model.on('all', this._onModelEvent, this);
};
// Internal method to create a model's ties to a collection.
Collection.prototype._addReference = function (model, options) {
this._index(model);
if (!model.collection) model.collection = this;
if (model.on) model.on('all', this._onModelEvent, this);
};
// Internal method to sever a model's ties to a collection.
Collection.prototype._removeReference = function(model, options) {
Collection.prototype._removeReference = function (model, options) {
if (this === model.collection) delete model.collection;
//model.off('all', this._onModelEvent, this);
this._deIndex(model);
if (model.off) model.off('all', this._onModelEvent, this);
};
Collection.prototype._onModelEvent = function (event, model, collection, options) {
if ((event === 'add' || event === 'remove') && collection !== this) return;
if (event === 'destroy') this.remove(model, options);
if (model && event === 'change:' + model.idAttribute) {
this._deIndex(model);
this._index(model);
}
this.trigger.apply(this, arguments);
};
Object.defineProperty(Collection.prototype, 'length', {

@@ -246,0 +261,0 @@ get: function () {

{
"name": "ampersand-collection",
"version": "0.1.1",
"version": "0.2.0",
"author": "Henrik Joreteg <henrik@andyet.net>",

@@ -9,10 +9,10 @@ "bugs": {

"dependencies": {
"ampersand-class-extend": "0.x.x",
"backbone-events-standalone": "0.x.x",
"backbone-extend-standalone": "0.x.x",
"extend-object": "0.x.x",
"is-array": "1.x.x",
"underscore": "1.x.x"
"is-array": "1.x.x"
},
"devDependencies": {
"run-browser": "",
"ampersand-state": "0.x.x",
"precommit-hook": "0.x.x",
"tape": "2.x.x"

@@ -34,5 +34,4 @@ },

"build": "browserify test/main.js -o test/bundle.js",
"start": "run-browser test/main.js",
"test": "node test/main.js"
}
}
var test = require('tape');
var Collection = require('../collection');
var underscoreMixins = require('../underscoreMixins');
var inBrowser = (typeof window !== 'undefined');
var restMixins = inBrowser ? require('../restMixins') : {};
var Collection = require('../ampersand-collection');
var State = require('ampersand-state');
var Stooge = State.extend({
props: {
id: 'string',
name: 'string'
}
});
test('basics', function (t) {

@@ -63,4 +68,4 @@ var c = new Collection();

test('extend: multi-extend for easy mixins', function (t) {
var hey = {hey: function () {return 'hey';}};
var hi = {hi: function () {return 'hi';}};
var hey = {hey: function () { return 'hey'; }};
var hi = {hi: function () { return 'hi'; }};
var C = Collection.extend(hey, hi);

@@ -70,3 +75,3 @@ var c = new C();

t.equal(c.hi(), 'hi');
var C2 = C.extend({woah: function () {return 'woah';}});
var C2 = C.extend({woah: function () { return 'woah'; }});
var c2 = new C2();

@@ -77,27 +82,26 @@ t.equal(c2.woah(), 'woah');

test('underscore mixins: should be able easily add underscore mixins', function (t) {
var C = Collection.extend(underscoreMixins);
var larry = {name: 'larry'};
var curly = {name: 'curly'};
var moe = {name: 'moe'};
var c = new C([larry, curly, moe]);
var filtered = c.filter(function (stooge) {
return stooge.name.length === 5;
test('add events', function (t) {
t.plan(2);
var c = new Collection();
var moe = new Stooge({name: 'moe'});
c.on('add', function (model, collection) {
t.equal(collection, c);
t.equal(model, moe);
});
t.equal(filtered.length, 2);
var sorted = c.sortBy('name');
t.equal(sorted[0], curly);
c.add(moe);
t.end();
});
// only run these in a browser (npm start open browser)
if (inBrowser) {
test('rest mixins: should be able to extend to be restful collection', function (t) {
var C = Collection.extend(restMixins, {
url: '/test/stooges.json'
});
var c = new C();
c.fetch();
test('remove events', function (t) {
t.plan(2);
var c = new Collection();
var moe = new Stooge({name: 'moe', id: 'thing'});
c.add(moe);
c.on('remove', function (model, collection) {
console.log("REMOVED");
t.equal(collection, c);
t.equal(model, moe);
});
}
c.remove(moe);
t.end();
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc