New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ampersand-collection

Package Overview
Dependencies
Maintainers
4
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 1.4.2 to 1.4.3

38

ampersand-collection.js

@@ -278,13 +278,29 @@ var BackboneEvents = require('backbone-events-standalone');

_deIndex: function (model) {
for (var name in this._indexes) {
delete this._indexes[name][model.hasOwnProperty(name) ? model[name] : (model.get && model.get(name))];
_deIndex: function (model, attribute, value) {
var indexVal;
if (attribute !== undefined) {
if (undefined === this._indexes[attribute]) throw new Error('Given attribute is not an index');
delete this._indexes[attribute][value];
return;
}
// Not a specific attribute
for (attribute in this._indexes) {
indexVal = model.hasOwnProperty(attribute) ? model[attribute] : (model.get && model.get(attribute));
delete this._indexes[attribute][indexVal];
}
},
_index: function (model) {
for (var name in this._indexes) {
var indexVal = model.hasOwnProperty(name) ? model[name] : (model.get && model.get(name));
if (indexVal != null) this._indexes[name][indexVal] = model;
_index: function (model, attribute) {
var indexVal;
if (attribute !== undefined) {
if (undefined === this._indexes[attribute]) throw new Error('Given attribute is not an index');
indexVal = model[attribute] || (model.get && model.get(attribute));
if (indexVal) this._indexes[attribute][indexVal] = model;
return;
}
// Not a specific attribute
for (attribute in this._indexes) {
indexVal = model.hasOwnProperty(attribute) ? model[attribute] : (model.get && model.get(attribute));
if (indexVal != null) this._indexes[attribute][indexVal] = model;
}
},

@@ -307,7 +323,9 @@

_onModelEvent: function (event, model, collection, options) {
var attribute = event.split(':')[1];
event = event.split(':')[0];
if ((event === 'add' || event === 'remove') && collection !== this) return;
if (event === 'destroy') this.remove(model, options);
if (model && event === 'change:' + this.mainIndex) {
this._deIndex(model);
this._index(model);
if (model && event === 'change' && this._indexes[attribute]) {
this._deIndex(model, attribute, model.previousAttributes()[attribute]);
this._index(model, attribute);
}

@@ -314,0 +332,0 @@ this.trigger.apply(this, arguments);

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

@@ -5,0 +5,0 @@ "bugs": {

@@ -474,4 +474,34 @@ var test = require('tape');

t.equal(c.length, 0, 'should be able to remove model by property');
t.end();
});
test('Bug 45. Should update indexes if an indexed attribute of a model change', function (t) {
t.plan(9);
var C = Collection.extend({
model: Stooge,
indexes: ['name']
});
var model = new Stooge({id: '1', name: 'moe'});
var collection = new C(model);
t.equal('1', collection.get('1').id, 'should find model with mainindex');
t.equal('1', collection.get('moe', 'name').id, 'should find model with other index');
model.id = '2';
t.equal('2', collection.get('2').id, 'should find model with new value of mainIndex');
t.equal(undefined, collection.get('1'), 'should not find model with old value of mainIndex');
model.name = 'larry';
t.equal('2', collection.get('larry', 'name').id, 'should find model with new value of other index');
t.equal(undefined, collection.get('moe', 'name'), 'should not find model with old value of other index');
model.unset('name');
t.equal('2', collection.get('2').id, 'should find model with mainIndex after unset other index');
t.equal(undefined, collection.get('moe', 'name'), 'should not find model with old value of other index after unset this attribute');
model.name = 'curly';
t.equal('2', collection.get('curly', 'name').id, 'should find model with new value of other index after unset/set');
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