ampersand-collection
Advanced tools
Comparing version 1.4.1 to 1.4.2
@@ -26,4 +26,2 @@ var BackboneEvents = require('backbone-events-standalone'); | ||
indexes: [], | ||
isModel: function (model) { | ||
@@ -83,3 +81,3 @@ return this.model && model instanceof this.model; | ||
} else { | ||
id = attrs[targetProto.idAttribute || this.mainIndex]; | ||
id = attrs[this.mainIndex]; | ||
} | ||
@@ -165,5 +163,5 @@ | ||
get: function (query, indexName) { | ||
if (!query) return; | ||
if (query == null) return; | ||
var index = this._indexes[indexName || this.mainIndex]; | ||
return index[query] || index[query[this.mainIndex]] || this._indexes.cid[query] || this._indexes.cid[query.cid]; | ||
return (index && (index[query] || index[query[this.mainIndex]])) || this._indexes.cid[query] || this._indexes.cid[query.cid]; | ||
}, | ||
@@ -254,3 +252,3 @@ | ||
_reset: function () { | ||
var list = this.indexes || []; | ||
var list = slice.call(this.indexes || []); | ||
var i = 0; | ||
@@ -286,3 +284,3 @@ list.push(this.mainIndex); | ||
for (var name in this._indexes) { | ||
delete this._indexes[name][model[name] || (model.get && model.get(name))]; | ||
delete this._indexes[name][model.hasOwnProperty(name) ? model[name] : (model.get && model.get(name))]; | ||
} | ||
@@ -293,4 +291,4 @@ }, | ||
for (var name in this._indexes) { | ||
var indexVal = model[name] || (model.get && model.get(name)); | ||
if (indexVal) this._indexes[name][indexVal] = model; | ||
var indexVal = model.hasOwnProperty(name) ? model[name] : (model.get && model.get(name)); | ||
if (indexVal != null) this._indexes[name][indexVal] = model; | ||
} | ||
@@ -297,0 +295,0 @@ }, |
{ | ||
"name": "ampersand-collection", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
@@ -5,0 +5,0 @@ "bugs": { |
@@ -403,1 +403,75 @@ var test = require('tape'); | ||
}); | ||
test('should not leak indexes between collections when indexes is undefined', function (t) { | ||
var data = [{id: 4, name: 'me'}]; | ||
var C = Collection.extend({ | ||
mainIndex: 'id' | ||
}); | ||
var D = Collection.extend({ | ||
mainIndex: 'name' | ||
}); | ||
var c = new C(); | ||
var d = new D(); | ||
c.reset(data); | ||
d.reset(data); | ||
t.ok(c.get(4), 'should get by mainIndex'); | ||
t.ok(d.get('me'), 'should get by mainIndex'); | ||
t.notOk(c.get('me', 'name'), 'should be undefined for invalid index'); | ||
t.notOk(d.get(4, 'me'), 'should throw an error for invalid index'); | ||
t.equal(d.get('me'), d.at(0), 'should get the same model by different indexes'); | ||
t.end(); | ||
}); | ||
test('should be able to get/remove a model with an idAttribute of 0', function (t) { | ||
var C = Collection.extend({ | ||
mainIndex: 'id', | ||
indexes: ['username'] | ||
}); | ||
var c = new C(); | ||
var moe = {id: 0, username: 'moe'}; | ||
var curly = {id: 1, username: 'curly'}; | ||
c.add([moe, curly]); | ||
t.ok(c.get(1), 'should get by id:1'); | ||
t.ok(c.get('curly', 'username'), 'should get by secondary index'); | ||
t.ok(c.get('moe', 'username'), 'should get by secondary index'); | ||
t.ok(c._indexes.id[0], 'should get by id:0'); | ||
t.ok(c.get(0, 'id'), 'should get by id:0'); | ||
t.ok(c.get(0), 'should get by id:0'); | ||
t.equal(moe, c.get(0)); | ||
c.remove(0); | ||
t.notOk(c.get(0), 'should remove by id:0'); | ||
t.end(); | ||
}); | ||
test('should check for existing by mainIndex and not model.idAttribute', function (t) { | ||
var C = Collection.extend({ | ||
mainIndex: 'name', | ||
model: Stooge | ||
}); | ||
var c = new C(); | ||
var moe = new Stooge({id: '0', name: 'moe'}); | ||
var curly = {id: '1', name: 'curly'}; | ||
c.add([moe, curly]); | ||
t.notEqual(c.mainIndex, moe.idAttribute, 'mainIndex can be different than idAttribute'); | ||
t.equal(moe.idAttribute, 'id', 'default model attribute should be id'); | ||
t.equal(moe.getId(), '0', 'default model attribute should be id'); | ||
t.ok(c.get('curly'), 'should get model using mainIndex'); | ||
t.equal(c.get('moe'), moe, 'should get same model using mainIndex'); | ||
t.equal(c.length, 2, 'should be only 2 models'); | ||
c.add(curly); | ||
t.equal(c.length, 2, 'should not add duplicate'); | ||
c.remove(moe); | ||
t.equal(c.length, 1, 'should be able to remove model'); | ||
c.remove(curly); | ||
t.equal(c.length, 0, 'should be able to remove model by property'); | ||
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
43911
726