jeromegn / Backbone.localStorage

A localStorage adapter for Backbone.js
MIT License
1.89k stars 681 forks source link

Duplicate values in records when calling create method multiple times #187

Closed hendiko closed 7 years ago

hendiko commented 9 years ago

If some one coincidentally calls create() method with models that have some id, it would continuously add the id into records. I think it might be a potential problem when some model with a long id.

Example:

> foo = new Store('foo')
> B…e.L…e.w…w.Store {name: "foo", records: Array[0]}
> foo.records
> []
> foo.create({id: 1})
> Object {id: 1}
> foo.create({id: 1})
> Object {id: 1}
> foo.records
> ["1", "1"]

I added a line in create method to prevent from adding duplicate ids into records.

this.records.indexOf(model.id.toString()) < 0 && this.records.push(model.id.toString());
scott-w commented 7 years ago

Good spot on this @hendiko. The localStorage adapter should map onto Collection's expected functionality of maintaining ID uniqueness.