ampersand-collection
Advanced tools
Comparing version 1.4.2 to 1.4.3
@@ -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(); | ||
}); |
46175
767