Skip to content

Commit

Permalink
Handles multiple items with the same (#57)
Browse files Browse the repository at this point in the history
* Handles multiple items with the same

* Refactor multiple items with same id handling
  • Loading branch information
megheaiulian authored and nomego committed Jun 25, 2018
1 parent dc43edf commit 0aff2e1
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cosmoz-data-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,19 @@
* @return {void}
*/
setItemById(id, item) {
const index = this.items
.findIndex(item => this._getItemId(item) === id);
const items = this.items,
matches = items.filter(item => this._getItemId(item) === id);

if (index < 0) {
if (matches.length === 0) {
console.warn('trying to replace an item that is not in the list', id, item);
return;
} else if (matches.length > 1) {
console.warn('found multiple items with same id');
}

this.set(['items', index], this._cache[id] = item);
this._cache[id] = Object.assign({}, item);
matches.forEach(match => this.set(['items', items.indexOf(match)], Object.assign({}, item)));

this._preload();

if (this.animating || this.selected == null) {
Expand Down

0 comments on commit 0aff2e1

Please sign in to comment.