lib/assets/javascripts/googleplus.photo.js.coffee in googleplus-reader-0.0.3 vs lib/assets/javascripts/googleplus.photo.js.coffee in googleplus-reader-0.0.4
- old
+ new
@@ -1,33 +1,40 @@
class Photo
constructor: (@attributes) ->
+ @$ = jQuery
- load: (options, callback) ->
- options ||= {}
+ load: (options = {}) ->
+ deferred = @$.Deferred()
width = options.width if options.width?
if @attributes.width?
if width?
- width = Math.min width, @attributes.width
+ width = Math.min(width, @attributes.width)
else
width = @attributes.width
if width?
- width = Math.round width
- url = @attributes.url.replace /w\d+-h\d+(-p)?/, "w#{ width }"
+ width = Math.round(width)
+ url = @attributes.url.replace(/w\d+-h\d+(-p)?/, "w#{width}")
else
url = @attributes.url
- element = $('<img/>')
+ element = @$('<img/>')
- if callback?
- element.on 'load', ->
- element.off 'load'
- callback element
+ element
+ .on 'load', ->
+ element.off('load error')
+ deferred.resolve(element)
+ return
- element.attr src: url
+ .on 'error', (details...) ->
+ element.off('load error')
+ deferred.reject(details...)
+ return
- element
+ element.attr(src: url)
+
+ deferred.promise()
window.GooglePlus ||= {}
window.GooglePlus.Photo = Photo