Skip to content

Instantly share code, notes, and snippets.

@nanuclickity
Created January 31, 2015 22:37
Show Gist options
  • Save nanuclickity/8e594cce912ef7d2a705 to your computer and use it in GitHub Desktop.
Save nanuclickity/8e594cce912ef7d2a705 to your computer and use it in GitHub Desktop.
Automate view loading in layout regions on render
define ['marionette', 'radio'], (Marionette, Radio)->
class AutoLayout extends Marionette.Behavior
defaults:
regionViews: {}
regionViewsOptions: {}
events:
loadStart: 'loader:start'
loadStop: 'loader:stop'
onRender: ->
rViews = _.result @options.regionViews
unless _.isObject(rViews) and _.keys(rViews).length
return throw "Invalid regionViews"
_(rViews).each @attachView
attachView: (rURL, rName)->
options = _.result(@options.regionViewsOptions)[rName]
region = @view.getRegion rName
evtStart = @options.events.loadStart
evtStop = @options.events.loadStop
@view.trigger evtStart, rURL, region
requirejs [rURL], (View)=>
view = new View(options)
region.show view
@view.trigger evtStop, view, region
window.Behaviors.AutoLayout = AutoLayout
return AutoLayout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment