Skip to content

Instantly share code, notes, and snippets.

@jblashill
Last active December 30, 2015 20:29
Show Gist options
  • Save jblashill/7880741 to your computer and use it in GitHub Desktop.
Save jblashill/7880741 to your computer and use it in GitHub Desktop.
When working with backbone, marionette and twitter bootstrap, often I want to create a Marionette.Layout that maps a Twitter bootstrap row. Here's a simple function to generate a Layout from column sizes + names.
var bootstrapRow = function(cols, names) {
var snippets = _.map(cols, function(col) {
return "<div class='col-md-" + col + "'></div>";
});
var regions = {};
_.each(names, function(name, index) {
regions[name] = ">:nth-child(" + (index+1) + ")";
});
var GridLayout = Backbone.Marionette.Layout.extend({
className: "row",
template: _.template(snippets.join("")),
regions: regions
});
return new GridLayout();
};
// ... example usage:
var rowView = bootstrapRow([4, 8], ["sidebar", "content"]);
$(document.body).append(rowView.el);
rowView.render();
rowView.sidebar.show(mySidebarView);
rowView.content.show(myContentView);
<!-- DOM generated by bootstrap row layout view -->
<div class="row">
<div class="col-md-4">
<!-- mySideBarView DOM -->
</div>
<div class="col-md-8">
<!-- myContentView DOM -->
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment