Progressive enhancement example
by davidsulc
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.js"></script>
<script type="text/template" id="angry_cats-template">
<thead>
<tr class='header'>
<th>Name</th>
</tr>
</thead>
<tbody>
</tbody>
</script>
<script type="text/template" id="angry_cat-template">
<td><%= name %></td>
</script>
<div id="content">
</div>
CSS
/*!
* Bootstrap v2.0.2
*
* Copyright 2012 Twitter, Inc
* Licensed under the Apache License v2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Designed and built with all the love in the world @twitter by @mdo and @fat.
*/
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
nav,
section {
display: block;
}
audio,
canvas,
video {
display: inline-block;
*display: inline;
*zoom: 1;
}
audio:not([controls]) {
display: none;
}
html {
font-size: 100%;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
a:focus {
outline: thin dotted #333;
outline: 5px auto -webkit-focus-ring-color;
outline-offset: -2px;
}
a:hover,
a:active {
outline: 0;
}
sub,
sup {
position: relative;
font-size: 75%;
line-height: 0;
vertical-align: baseline;
}
sup {
top: -0.5em;
}
sub {
bottom: -0.25em;
}
img {
height: auto;
border: 0;
-ms-interpolation-mode: bicubic;
vertical-align: middle;
}
button,
input,
select,
textarea {
margin: 0;
font-size: 100%;
vertical-align: middle;
}
button,
input {
*overflow: visible;
line-height: normal;
}
button::-moz-focus-inner,
input::-moz-focus-inner {
padding: 0;
border: 0;
}
button,
input[type="button"],
input[type="reset"],
input[type="submit"] {
cursor: pointer;
-webkit-appearance: button;
}
input[type="search"] {
-webkit-appearance: textfield;
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
}
input[type="search"]::-webkit-search-decoration,
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: none;
}
textarea {
overflow: auto;
vertical-align: top;
}
.clearfix {
*zoom: 1;
}
.clearfix:before,
.clearfix:after {
display: table;
content: "";
}
.clearfix:after {
clear: both;
}
.hide-text {
overflow: hidden;
text-indent: 100%;
white-space: nowrap;
}
.input-block-level {
display: block;
width: 100%;
min-height: 28px;
/* Make inputs at least the height of their button...
JavaScript
MyApp = new Backbone.Marionette.Application();
MyApp.addRegions({
mainRegion: "#content"
});
AngryCat = Backbone.Model.extend({});
AngryCats = Backbone.Collection.extend({
model: AngryCat
});
AngryCatView = Backbone.Marionette.ItemView.extend({
template: "#angry_cat-template",
tagName: 'tr',
className: 'angry_cat'
});
AngryCatsView = Backbone.Marionette.CompositeView.extend({
tagName: "table",
id: "angry_cats",
className: "table-striped table-bordered",
template: "#angry_cats-template",
itemView: AngryCatView,
appendHtml: function(collectionView, itemView){
collectionView.$("tbody").append(itemView.el);
}
});
MyApp.addInitializer(function(options) {
var view;
if(options.view){
console.log("Prebuilt view");
view = options.view;
}
else if(options.cats){
console.log("Building new view");
view = new AngryCatsView({
collection: options.cats
});
}
else{
alert("No view to display");
}
MyApp.mainRegion.show(view);
});
$(document).ready(function() {
// Here we create a bunch of models at the same time as the collection.
var cats = new AngryCats([
new AngryCat({
name: 'Wet Cat'
}),
new AngryCat({
name: 'Bitey Cat'
}),
new AngryCat({
name: 'Surprised Cat'
})
]);
var view = new AngryCatsView({collection: cats});
MyApp.start({
//cats: cats //use collection to build the view that will be displayed
view: view //display a prebuilt view, which would typically be used in a case of progressive enhancement
});
});