Sha256: 77b0b101078d370c3bc580bf02c44fd7386969171493690685cb754ad90889b2

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

handleLiveReloadEvent = (ev) ->
  try
    code = ev.data.contents
    filePath = ev.data.url
    isHTML = code.substr(0,6) is "HTML>>"
    isCSS = filePath.indexOf('.css') > -1

    fileClassName = filePath.replace(/^.*\/(.*)\.js/, "$1")
    className = Batman.helpers.camelize(fileClassName)
    appClass = Batman.currentApp[className]

    if isHTML
      htmlPath = code.substr(6)
      console.debug "HTML Reload: #{htmlPath}"
      Batman.View.liveReloadHTML()

    else if isCSS
      cssPath = filePath.substr(6)
      console.debug "CSS Reload: #{cssPath}"
      queryString = '?reload=' + new Date().getTime();
      found = false
      # search all stylesheets for the one that got reloaded
      $('link[rel="stylesheet"]').each ->
        if this.href.indexOf(cssPath) > -1
          found = this

      if found # then force reload by updating href
        found.href = found.href.replace(/\?.*|$/, queryString)
      else
        console.warn("NO CSS FOUND FOR #{cssPath}")

    # Classes can implement .liveReload hooks
    else if appClass?.liveReload
      console.debug "#{className} Reload: #{ev.data.url}"
      appClass.liveReload?(className, code)
      Batman.currentApp.liveReloadClass(className)

    # handle a plain-js class by just eval'ing the code
    # (for example, if you have some plain-coffeescript classes in your app)
    else if appClass? && !appClass.__super__?
      console.debug "Plain JS Reload: #{className} with #{ev.data.url}"
      eval(code)

    else
      console.warn "Couldn't reload #{filePath}."

    # you can hook your own handler up to it if you want
    Batman.currentApp.fire("liveReload", ev)

  catch err
    console.warn "handleLiveReloadEvent Error: #{err}", err

if !window._batmanLiveReload
  window.addEventListener('fb-flo-reload', handleLiveReloadEvent)
  window._batmanLiveReload = true # only attach the listener once, just in case

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
batman-rails-flo-0.0.5 vendor/assets/javascripts/batman/reload_event_handler.js.coffee
batman-rails-flo-0.0.4 vendor/assets/javascripts/batman_rails_flo/reload_event_handler.js.coffee
batman-rails-flo-0.0.3 vendor/assets/javascripts/batman_rails_flo/reload_event_handler.js.coffee
batman-rails-flo-0.0.2 vendor/assets/javascripts/batman_rails_flo/reload_event_handler.js.coffee