From 8b65f35def08fe61d252b5ef255a5a26ddbcefe0 Mon Sep 17 00:00:00 2001 From: Chris Andrejewski Date: Mon, 24 Apr 2017 12:06:23 -0400 Subject: [PATCH] Make IDs always unique; fixes #102 --- helpers/legacyStore.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/helpers/legacyStore.js b/helpers/legacyStore.js index a4304ae..a7f3575 100644 --- a/helpers/legacyStore.js +++ b/helpers/legacyStore.js @@ -6,10 +6,14 @@ var assign = require("can-util/js/assign/assign"); module.exports = function (count, make, filter) { /*jshint eqeqeq:false */ - // check - // the currentId to use when a new instance is created. - var currentId = 0, - items, + var getUniqueId = (function () { + var i = 0; + return function () { + return i++; + } + })(); + + var items, findOne = function (id) { for (var i = 0; i < items.length; i++) { if (id == items[i].id) { @@ -43,9 +47,8 @@ module.exports = function (count, make, filter) { var item = make(i, items); if (!item.id) { - item.id = i; + item.id = getUniqueId(); } - currentId = Math.max(item.id + 1, currentId + 1) || items.length; items.push(item); } }; @@ -253,7 +256,7 @@ module.exports = function (count, make, filter) { // If an ID wasn't passed into the request, we give the item // a unique ID. if (!item.id) { - item.id = currentId++; + item.id = getUniqueId(); } // Push the new item into the store.