From ffb5e0dfdafccd857cf2cc3e0bafad7a1a478d9b Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Wed, 20 Jan 2016 17:27:06 -0800 Subject: [PATCH] Optimize offsetOfId, shouldn't throw if models contains undefined --- src/flux/models/query-result-set.coffee | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/flux/models/query-result-set.coffee b/src/flux/models/query-result-set.coffee index f5a8997e6..a8a71da15 100644 --- a/src/flux/models/query-result-set.coffee +++ b/src/flux/models/query-result-set.coffee @@ -78,7 +78,13 @@ class QueryResultSet @_modelsHash[id] offsetOfId: (id) -> - idx = _.findIndex @models(), (m) -> m.id is id or m.clientId is id + idx = @_ids.indexOf(id) + + # If we can't find the item, try to match against client ids as well. Some + # items in the models() array may not be loaded, but we can try our best. + if idx is -1 + idx = _.findIndex @models(), (m) -> m and (m.id is id or m.clientId is id) + return -1 if idx is -1 return @_offset + idx