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.
This commit is contained in:
Andrew Miner
2015-11-15 11:42:19 -08:00
parent ff821bd4a2
commit 3ec45c6b0e
+22 -6
View File
@@ -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()