Render a model using template:
MyItemView = Marionette.ItemView.extend({
template: _.template('Hello, <%= name %>')
});
new MyItemView({ model: … }).render();
Loop through collection, render an ItemView per model:
MyCollectionView = Marionette.CollectionView.extend({
itemView: MyItemView
});
new MyCollectionView({ collection: … }).render();
Loop through collection, ItemView per model, with context:
MyCompositeView = Marionette.CompositeView.extend({
template: _.template('<thead>…</thead><tbody></tbody>'),
itemViewContainer: 'tbody',
itemView: MyItemView
});
new MyCompositeView({ collection: … }).render();
+--------------------------------------+
| <img src="logo.png"> etc |
+--------------------------------------+
| <div id="menu-bar"></div> |
+--------------------------------------+
| <div id="main-content"> |
| |
| |
| </div> |
+--------------------------------------+
| <footer>copyright statement</footer> |
+--------------------------------------+
MyLayout = Marionette.Layout.extend({
template: "#my-layout-template",
regions: {
menu: "#menu-bar",
content: "#main-content"
}
});
layout = new MyLayout();
layout.render();
layout.menu.show(new MenuView());
layout.content.show(new ContentView());
Allows to add further abstraction levels as necessary.
MyApp = new Backbone.Marionette.Application();
MyApp.addRegions({
mainRegion: "#main"
});
var layout = new AppLayout();
MyApp.mainRegion.show(layout);
layout.show(new MenuView());
You can also add function to be called on startup:
MyApp.addInitializer(function(options){
new MyAppRouter();
Backbone.history.start();
});
The app object also has an event bus (called vent):
MyApp.vent.on("foo", function(){
alert("bar");
});
MyApp.vent.trigger("foo");
And, finally, to start the damn thing:
options = { foo: "bar" };
MyApp.start(options);