MarionetteJS (Backbone.Marionette) Playground
A base template to use for building Backbone and Marionette applications.
http://marionettejs.com
HTML
<script src="http://underscorejs.org/underscore.js"></script>
<script src="http://backbonejs.org/backbone.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.marionette/3.3.1/backbone.marionette.js"></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<header>
<h1>CompositeView demo</h1>
</header>
</br>
<script type="text/html" id="movie-list">
<div class = "panel panel-primary">
<div class = "panel-heading" >
<h3 class = "panel-title" > Composite view: Movie listing </h3>
</div >
<div class = "panel-body" ></div>
</div >
</script>
<script type="text/template" id="movie-list-item">
Title: <%= title %> . <button type="button"
class="btn btn-success some-button"> <span class="some-button">Click here </span></button>
</script>
<div id="container"></div>
CSS
li {
height: 40px;
}
li > button {
float: right;
}
JavaScript
// Define a collection
var col = Backbone.Collection.extend({});
var colInstance = new col();
colInstance.set([{
title: "Braveheart"
}, {
title: "Gone in 59 seconds!"
}, {
title: "Top gun"
}, {
title: "Family guy"
}, {
title: "Titanic"
}, {
title: "Snakes on a plane :/"
}]);
// Itemview
var movieItemView = Marionette.ItemView.extend({
tagName: "li",
className: 'listItem',
template: "#movie-list-item",
ui: {
viewButton: '.some-button'
},
events: {
'click @ui.viewButton': 'clickHandler'
},
clickHandler: function(ev) {
// Log the click
console.log('There was a click in an itemView!');
// Uncomment the following to prevent multiple events:
//ev.stopPropagation();
}
});
// Composite view
var movieCompView = Marionette.CompositeView.extend({
template: "#movie-list",
itemViewContainer: ".panel-body",
itemView: movieItemView,
ui: {
viewButton: '.some-button'
},
events: {
'click @ui.viewButton': 'clickHandler'
},
clickHandler: function(ev) {
// Log the click
console.log('There was a click in a collectionView!');
// Uncomment the following to prevent multiple events:
//ev.stopPropagation();
}
});
// Create a region
var rm = new Marionette.RegionManager();
rm.addRegion("container", "#container");
// Create instance of composite view
var movieCompViewInstance = new movieCompView({
collection: colInstance
});
// Show the collectionView
rm.get('container').show(movieCompViewInstance);