Skip to content

Instantly share code, notes, and snippets.

@loopj
Created October 8, 2012 21:44
Show Gist options
  • Save loopj/3855205 to your computer and use it in GitHub Desktop.
Save loopj/3855205 to your computer and use it in GitHub Desktop.
Batman.js controller which adds rails-style layouts to controllers
class Batman.LayoutController extends Batman.Controller
render: (options = {}) ->
layout = options.layout ? @layout ? Batman.helpers.underscore(@get('routingKey'))
if layout
# Prefetch the action's view in parallel
source = options.source || Batman.helpers.underscore(@get('routingKey') + '/' + @get('action'))
Batman.View.store.prefetch source
# Fetch the layout's view and apply context
layoutView = @renderCache.viewForOptions
viewClass: Batman.View
context: @get('_renderContext')
source: Batman.helpers.underscore('layouts/' + @get('routingKey'))
# Render the layout's view into the DOM, the action into the layout
layoutView.on 'ready', =>
Batman.DOM.Yield.withName(options.into || @defaultRenderYield).replace layoutView.get('node')
options.into = 'content' # TODO: Make customizable somehow
super(options)
else
super(options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment