Robert Mosolgo

To my knowledge, batman.js is not maintained. For that reason, I don't suggest that you use it for a new project!

Reload HTML for Batman.Views

When working on HTML for Batman.Views, it can be annoying to refresh and navigate back to wherever you were. Hacking into Batman.HTMLStore enables you to reload HTML without refreshing

The Code

You’ll want to include all this code _after batman.js and before your app._

# alter `Batman.HTMLStore`'s default accessor so that it isn't `final` and has an `unset` action:
storeAccessor = Batman.HTMLStore::_batman.getFirst('defaultAccessor')
storeAccessor.final = false
storeAccessor.unset = (path) ->
  if !path.charAt(0) is "/"
    path = "/#{path}"
  @_requestedPaths.remove(path)
  @_htmlContents[path] = undefined

# returns the next superview with a defined source
Batman.View::superviewWithSource = ->
  if @get('source')?
    return @
  else
    return @superview.superviewWithSource()

# Unset the view's HTML, then reload it and re-initialize the view when it loads
Batman.View::refreshHTML = ->
  # climb the view tree to find a view with a defined `source`
  @sourceView ?= @superviewWithSource()
  sourceView = @sourceView
  sourceView.html = undefined
  path = sourceView.get('source')
  if path.charAt(0) isnt "/"
    path = "/#{path}"
  Batman.View.store.unset(path)
  sourceView._HTMLObserver ?= Batman.View.store.observe path, (nv, ov) =>
    sourceView.set('html', nv)
    sourceView.loadView()
    sourceView.initializeBindings()

Now, you can call refreshHTML() on a Batman.View to reload its HTML from the server.

Do It

In Chrome:

  • right-click, “Inspect Element” on a HTML element. The element is now available at $0 in your console
  • $context($0).refreshHTML() to get the view for the node and call refreshHTML on it.

Cha-ching!