ampersand-collection
Advanced tools
Comparing version 1.3.13 to 1.3.14
@@ -13,2 +13,6 @@ var BackboneEvents = require('backbone-events-standalone'); | ||
if (options.parent) this.parent = options.parent; | ||
if (!this.mainIndex) { | ||
var idAttribute = this.model && this.model.prototype && this.model.prototype.idAttribute; | ||
this.mainIndex = idAttribute || 'id'; | ||
} | ||
this._reset(); | ||
@@ -22,4 +26,2 @@ this.initialize.apply(this, arguments); | ||
mainIndex: 'id', | ||
indexes: [], | ||
@@ -87,3 +89,3 @@ | ||
if (existing = this.get(id)) { | ||
if (remove) modelMap[existing.cid || existing.id] = true; | ||
if (remove) modelMap[existing.cid || existing[this.mainIndex]] = true; | ||
if (merge) { | ||
@@ -114,4 +116,4 @@ attrs = attrs === model ? model.attributes : attrs; | ||
if (!model) continue; | ||
if (order && ((model.isNew && model.isNew() || !model.id) || !modelMap[model.cid || model.id])) order.push(model); | ||
modelMap[model.id] = true; | ||
if (order && ((model.isNew && model.isNew() || !model[this.mainIndex]) || !modelMap[model.cid || model[this.mainIndex]])) order.push(model); | ||
modelMap[model[this.mainIndex]] = true; | ||
} | ||
@@ -123,3 +125,3 @@ | ||
model = this.models[i]; | ||
if (!modelMap[model.cid || model.id]) toRemove.push(model); | ||
if (!modelMap[model.cid || model[this.mainIndex]]) toRemove.push(model); | ||
} | ||
@@ -308,3 +310,3 @@ if (toRemove.length) this.remove(toRemove, options); | ||
if (event === 'destroy') this.remove(model, options); | ||
if (model && event === 'change:' + model.idAttribute) { | ||
if (model && event === 'change:' + this.mainIndex) { | ||
this._deIndex(model); | ||
@@ -311,0 +313,0 @@ this._index(model); |
{ | ||
"name": "ampersand-collection", | ||
"version": "1.3.13", | ||
"version": "1.3.14", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
@@ -5,0 +5,0 @@ "bugs": { |
@@ -50,3 +50,3 @@ # ampersand-collection | ||
Adding [ampersand-collection-rest-mixin](http://github.com/AmpersandJS/ampersand-rest-mixin) and [ampersand-collection-underscore-mixin](http://github.com/AmpersandJS/ampersand-collection-underscore-mixin). | ||
Adding [ampersand-collection-rest-mixin](https://github.com/AmpersandJS/ampersand-collection-rest-mixin) and [ampersand-collection-underscore-mixin](http://github.com/AmpersandJS/ampersand-collection-underscore-mixin). | ||
@@ -131,8 +131,14 @@ ```js | ||
Specify which key on your models should be used as their main unique identifier. Defaults to `id`. This is the default key that [`get`](#ampersand-collection-get) will use to retrieve models by, as well as the key add/set/remove will compare to determine whether a model exists in the collection or not. | ||
Specify which property on your models should be used by the collection as the main index and unique identifier for the models/objects it holds. This is the property that [`get`](#ampersand-collection-get) will use to retrieve models by and what `add`, `set`, and `remove` will use to determine whether a model already exists in the collection or not. | ||
If your server does not use `id` as the model identifier, you would typically override this when extending AmpersandCollection, like so: | ||
If you specify a [`model`](http://ampersandjs.com/docs#ampersand-collection-model) property in the collection and that model specifies an [`idAttribute`](http://ampersandjs.com/docs#ampersand-state-idattribute) the collection will use *that* as the `mainIndex` unless you set it to something else explicitly. | ||
If *no* `mainIndex` or `model` is specified `"id"` is used as the default `mainIndex`. | ||
This means, that *most* of the time you don't need to set `mainIndex` and things will still Just Work™. | ||
But if you wish, you may set it while extending AmpersandCollection like so: | ||
```javascript | ||
var People = AmpersandColletion.extend({ | ||
var People = AmpersandCollection.extend({ | ||
mainIndex: '_id' | ||
@@ -139,0 +145,0 @@ }); |
@@ -286,1 +286,40 @@ var test = require('tape'); | ||
}); | ||
test('Bug 20. Should prevent duplicate items when using non-standard idAttribute', function (t) { | ||
var data = [{_id: '2'}]; | ||
var Model = State.extend({ | ||
idAttribute: '_id', | ||
props: { | ||
_id: 'string' | ||
} | ||
}); | ||
var C = Collection.extend({ | ||
model: Model | ||
}); | ||
var c = new C(); | ||
c.reset(data); | ||
c.add(data); | ||
t.equal(c.length, 1, 'should have detected the dupe and not added'); | ||
t.end(); | ||
}); | ||
test('Bug 19. Should set mainIndex from model if supplied', function (t) { | ||
var Model = State.extend({ | ||
idAttribute: '_id', | ||
props: { | ||
_id: 'string' | ||
} | ||
}); | ||
var C = Collection.extend({ | ||
model: Model | ||
}); | ||
var c = new C(); | ||
t.equal(c.mainIndex, '_id', 'should have set mainIndex off of model'); | ||
var c2 = new Collection(); | ||
t.equal(c2.mainIndex, 'id', 'mainIndex should default to `id`'); | ||
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
38766
8
595
354