Fix image loader bug which caused broken links
This commit is contained in:
@@ -15,7 +15,7 @@ module.exports = class CraftingGridController extends BaseController
|
||||
|
||||
constructor: (options={})->
|
||||
if not options.model? then throw new Error 'options.model is required'
|
||||
options.imageLoader ?= new ImageLoader default:'/images/unknown.png'
|
||||
options.imageLoader ?= new ImageLoader defaultUrl:'/images/unknown.png'
|
||||
options.templateName = 'crafting_grid'
|
||||
super options
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ module.exports = class CraftingTableController extends BaseController
|
||||
constructor: (options={})->
|
||||
if not options.model? then throw new Error 'options.model is required'
|
||||
if not options.modPack? then throw new Error 'options.modPack is required'
|
||||
options.imageLoader ?= new ImageLoader default:'/images/unknown.png'
|
||||
options.imageLoader ?= new ImageLoader defaultUrl:'/images/unknown.png'
|
||||
options.templateName = 'crafting_table'
|
||||
super options
|
||||
|
||||
@@ -35,7 +35,9 @@ module.exports = class CraftingTableController extends BaseController
|
||||
# BaseController Overrides #####################################################################
|
||||
|
||||
onDidRender: ->
|
||||
@gridController = @addChild CraftingGridController, '.view__crafting_grid', model:@model.grid
|
||||
@gridController = @addChild CraftingGridController, '.view__crafting_grid',
|
||||
model: @model.grid
|
||||
imageLoader: @imageLoader
|
||||
|
||||
@$multiplier = @$('.multiplier')
|
||||
@$next = @$('.next')
|
||||
|
||||
@@ -12,13 +12,13 @@ All rights reserved.
|
||||
module.exports = class ImageLoader
|
||||
|
||||
constructor: (options={})->
|
||||
options.defaultUrl ?= ''
|
||||
options.defaultUrl ?= null
|
||||
options.onLoading ?= -> @hide()
|
||||
options.onLoad ?= -> @show()
|
||||
|
||||
@defaultUrl = options.defaultUrl
|
||||
@onLoading = options.onLoading
|
||||
@onLoad = options.onLoad
|
||||
@defaultUrl = options.defaultUrl
|
||||
@onLoading = options.onLoading
|
||||
@onLoad = options.onLoad
|
||||
|
||||
@_images = {}
|
||||
|
||||
@@ -45,10 +45,10 @@ module.exports = class ImageLoader
|
||||
$el.data 'isLoading', true
|
||||
$el.data 'isLoaded', false
|
||||
|
||||
if data.isLoaded?
|
||||
if data.isLoaded
|
||||
@_loadImageIntoElement data.imageUrl, $el
|
||||
else
|
||||
if @defaultUrl? then $el.attr 'src', @defaultUrl
|
||||
$el.removeAttr 'src'
|
||||
if data.elements.indexOf($el) is -1 then data.elements.push $el
|
||||
|
||||
return this
|
||||
@@ -59,9 +59,10 @@ module.exports = class ImageLoader
|
||||
data = imageUrl:imageUrl, elements:[], image:new Image, isLoaded:false
|
||||
@_images[imageUrl] = data
|
||||
|
||||
data.image = new Image
|
||||
data.image.onload = => @_onImageLoaded data
|
||||
data.image.src = imageUrl
|
||||
data.image = new Image
|
||||
data.image.onload = => @_onImageLoaded data
|
||||
data.image.onerror = => @_onImageError data
|
||||
data.image.src = imageUrl
|
||||
|
||||
return data
|
||||
|
||||
@@ -74,6 +75,16 @@ module.exports = class ImageLoader
|
||||
data.elements = []
|
||||
data.isLoaded = true
|
||||
|
||||
_onImageError: (data)->
|
||||
if @defaultUrl?
|
||||
data.imageUrl = @defaultUrl
|
||||
|
||||
for $el in data.elements
|
||||
@_loadImageIntoElement @defaultUrl, $el
|
||||
|
||||
data.elements = []
|
||||
data.isLoaded = true
|
||||
|
||||
_loadImageIntoElement: (imageUrl, $el)->
|
||||
$el.data 'isLoading', false
|
||||
$el.data 'isLoaded', true
|
||||
|
||||
@@ -22,6 +22,8 @@ module.exports = class ItemPageController extends BaseController
|
||||
options.templateName = 'item_page'
|
||||
super options
|
||||
|
||||
@imageLoader = options.imageLoader
|
||||
|
||||
# Event Methods ################################################################################
|
||||
|
||||
onToolsBoxToggled: ->
|
||||
@@ -55,8 +57,9 @@ module.exports = class ItemPageController extends BaseController
|
||||
title: "Items you'll need"
|
||||
|
||||
@craftingTableController = @addChild CraftingTableController, '.view__crafting_table',
|
||||
model: @model.table
|
||||
modPack: @model.modPack
|
||||
imageLoader: @imageLoader
|
||||
model: @model.table
|
||||
modPack: @model.modPack
|
||||
|
||||
@modPackController = @addChild ModPackController, '.view__mod_pack', model:@model.modPack
|
||||
|
||||
|
||||
Reference in New Issue
Block a user