Sha256: 8ccf3bae8bfefedfb269b71241bb6eaa10f10a14c13872713cc020d4d8987825

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

#
# Preloader for libraries using <script src> without any caching magic
#
# Example:
#   libraries = [['/test1.js'], ['/test2.js']]
#   InlinePreloader.load libraries,
#     start:    -> console.log 'preloading started'
#     complete: -> console.log 'preloading finished'
#
# @class InlinePreloader
#
@Preloader = @InlinePreloader =

  #
  # Loads set of libraries by adding <script src> to DOM head 
  # See class description for example of usage
  #
  # @param [Array] 2-levels array of libraries URLs i.e. [['/test1.js'],['/test2.js']]
  # @param [Hash] Available options:
  #   * start: `() -> null` to call before load starts: 
  #   * complete: `() -> null` to call after load completes
  #
  load: (libraries, options) ->    
    @[key] = val for key, val of options
    @start?()

    if libraries.length > 0
      @receive libraries.shift()[0], => @load(libraries)
    else
      @complete?()

  #
  # Loads one script by adding <script src> to DOM head
  #
  # @param [String] url to load script from
  # @param [Function] `() -> null` to call after script was loaded and executed
  #
  receive: (url, callback) ->
    head   = document.getElementsByTagName("head")[0]
    script = document.createElement "script"
    script.src = url

    done = false

    proceed = ->
      if !done && (!@readyState? || @readyState == "loaded" || @readyState == "complete")
        done = true
        callback?()
        script.onload = script.onreadystatechange = null

    script.onload = script.onreadystatechange = proceed

    head.appendChild script
    return undefined

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
joosy-0.1.0.RC2 app/assets/javascripts/joosy/preloaders/inline.js.coffee