backbone marionette layout's regions and subviews

Testing the communication between the layout and the subviews assigned to the layout's regions

by olivierntk

HTML

<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<script src="https://raw.github.com/derickbailey/backbone.marionette/master/lib/backbone.marionette.min.js"></script>
<div id="region"></div>

<script id="layout-tmpl" type="text/_">
    <h3>Heading</h3>
    <div id="content"></div>
    <div id="content2"></div>    
</script>

<script id="item-tmpl" type="text/_">
    <form>
        <label>Firstname:</lable>
        <input type="text" name="firstname" value="<%= firstname %>" />
        <input type="button" value="Save" id="save" />
    </form>    
</script>

CoffeeScript

SimpleLayout = Backbone.Marionette.Layout.extend 
    template: '#layout-tmpl' 
    regions:
        main: '#content'
        other: '#content2'
    initialize: () ->
        console.log @
    onShow: () ->
        _.each @.regionManagers, (region, name, something) =>
            console.log region.currentView
            # @.bindTo region.currentView, "custom:event", @callback           
    handler: () ->
        alert "HELL YEAH"
       
SimpleItemView = Backbone.Marionette.ItemView.extend
    template: "#item-tmpl"
    events:
        'click #save': 'save'
    save: (evt) ->             
        evt.preventDefault()
        @.trigger "custom:event"


region = new Backbone.Marionette.Region el: "#region"    
layout = new SimpleLayout() 
region.show layout
layout.main.show new SimpleItemView model: (new Backbone.Model firstname: "Olivier")
layout.other.show new SimpleItemView model: (new Backbone.Model firstname: "Travis")