MarionetteJS (Backbone.Marionette) Playground
A base template to use for building Backbone and Marionette applications. http://marionettejs.com
by Cardiff
HTML
<script src="http://underscorejs.org/underscore.js"></script>
<script src="http://backbonejs.org/backbone.js"></script>
<script src="http://marionettejs.com/downloads/backbone.marionette.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<header>
<h1>CompositeView demo</h1>
</header>
</br>
<div id="container"></div>
<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-danger"> Delete </button>
</script>
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",
template: "#movie-list-item",
ui: {
removebtn: '.btn-danger'
},
triggers: {
'click @ui.removebtn': 'movie:delete'
}
});
// Composite view
var movieCompView = Marionette.CompositeView.extend({
template: "#movie-list",
itemViewContainer: ".panel-body",
itemView: movieItemView
});
// 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);
// Controller event listeners
movieCompViewInstance.on("itemview:movie:delete", function (view) {
// Log all arguments
if(console){
console.log.apply(console, arguments);
}
// Remove model from collection
this.collection.remove(view.model);
});