From 3ec45c6b0ecba865b85023c0e32c6c161bc5cd6b Mon Sep 17 00:00:00 2001 From: Andrew Miner Date: Sun, 15 Nov 2015 11:42:19 -0800 Subject: [PATCH] Re-write `Inventory.localize` to avoid a bug The bug was that arriving at the crafting page by following a link wouldn't show the correct URL. The crafting would still work, but the URL would be incorrect. --- src/coffee/models/inventory.coffee | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/coffee/models/inventory.coffee b/src/coffee/models/inventory.coffee index ed1749a5b..645e8085b 100644 --- a/src/coffee/models/inventory.coffee +++ b/src/coffee/models/inventory.coffee @@ -76,21 +76,37 @@ module.exports = class Inventory extends BaseModel localize: -> if not @modPack? then throw new Error 'localize requires @modPack' + changed = false newSlugs = [] + newStacks = [] for itemSlug in @_itemSlugs stack = @_stacks[itemSlug] + continue unless stack? + + qualifiedSlug = if itemSlug.isQualified then itemSlug else null + if not qualifiedSlug? + qualifiedSlug = @modPack.findItem(itemSlug)?.slug + changed = qualifiedSlug? - qualifiedSlug = @modPack.findItem(itemSlug)?.slug if qualifiedSlug? - delete @_stacks[itemSlug] newSlugs.push qualifiedSlug - @_stacks[qualifiedSlug] = stack - stack.itemSlug = qualifiedSlug + newStacks.push new Stack itemSlug:qualifiedSlug, stack.quantity else newSlugs.push itemSlug + newStacks.push stack - @_itemSlugs = newSlugs - @_sort() + if changed + for itemSlug, stack of @_stacks + @stopListening stack + + @_itemSlugs = newSlugs + @_stacks = {} + for stack in newStacks + @_stacks[stack.itemSlug] = stack + @listenTo stack, Event.change, => @trigger Event.change, this + + @_sort() + @trigger Event.change, this pop: -> itemSlug = @_itemSlugs.pop()