Skip to content

Instantly share code, notes, and snippets.

@jacquescrocker
Created September 9, 2011 21:09
Show Gist options
  • Save jacquescrocker/1207345 to your computer and use it in GitHub Desktop.
Save jacquescrocker/1207345 to your computer and use it in GitHub Desktop.
# DEV RELOAD FOR JQUERY / UNDERSCORE
$ = jQuery
# add reloadElement to underscore
_.mixin
# reloads a particular element
reloadElement: (element) ->
if element and element.tagName
tag = element.tagName.toUpperCase()
# check if its a link or a script
if tag == "LINK"
srcAttribute = "href"
else if tag == "SCRIPT"
srcAttribute = "src"
# make the swap
if srcAttribute
href = element[srcAttribute].replace(/[?&]reloader=([^&$]*)/,'')
sep = if href.indexOf("?") >= 0 then "&" else "?"
element[srcAttribute] = "#{href}#{sep}reloader=#{new Date().valueOf()}"
return element
$(document).ready ->
return unless typeof(pusher) == "object"
return unless Settings.env == "development"
console.log("Setting up dev_reload")
# setup guardfile
pusher.subscribe('guard-pusher').bind 'guard', (e) ->
# console.log("files have been updated")
# console.log(e)
reload = false
# cycle through paths
_(e.paths || []).each (path) ->
console.log("changed: #{path}")
if _(path).endsWith(".css") or _(path).endsWith(".sass") or _(path).endsWith(".scss")
# lets try to dynamically replace this css file
reload = false
console.log("Reloading Inline CSS")
# swap css files
_(document.querySelectorAll('link[rel=stylesheet][href]')).each (element) ->
_(element).reloadElement()
else
reload = true
if reload
console.log("RELOAD THE PAGE!")
window.location.reload()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment