Created by Dmytro Yarmak
You're not required to use all of Marionette.
You can pick what you need.
To read: Use cases for the different views
The base View type that other Marionette views extend from (not intended to be used directly)
This is the most basic of view types.
Use to render one model or collection.
MyView = Marionette.ItemView.extend({
template: "#my-template"
});
myView = new MyView();
myView.render();
Render ItemView instances for each model
MyItemView = Marionette.ItemView.extend({
template: "#item-template"
});
MyCollectionView = Marionette.CollectionView.extend({
itemView: MyItemView
});
A CompositeView is a collection view that knows how to render a template to wrap around the individual items.
Composite views can also be used to build hierarchical and recursive structures.
MyItemView = Marionette.ItemView.extend({
template: "#item-template"
});
MyCompositeView = Marionette.CompositeView.extend({
templare: '#composite-template',
itemView: MyItemView,
itemViewContainer: '.list-of-items'
});
A view that renders a layout and creates region managers to manage areas within it
AppLayout = Backbone.Marionette.Layout.extend({
template: "#layout-template",
regions: {
menu: "#menu",
content: "#content"
}
});
var layout = new AppLayout();
layout.render();
layout.menu.show(new MenuView());
layout.content.show(new MainContentView());
var myRegion = new Marionette.Region({
el: "#someElement"
});
Show the first view.
var myView = new MyView();
myRegion.show(myView);
Replace the view with another. The `close` method is called for you
var anotherView = new AnotherView();
myRegion.show(anotherView);
An application object that starts your app via initializers, and more
MyApp = new Marionette.Application();
MyApp.addInitializer(function(options){
// do useful stuff here
});
MyApp.addRegions({
someRegion: "#some-div",
anotherRegion: "#another-div"
});
MyApp.start(options);
MyApp.module("MyModule", function(MyModule, MyApp, Backbone, Marionette, $, _){
// Private Data And Functions
// --------------------------
var myData = "this is private data";
var myFunction = function(){
console.log(myData);
}
// Public Data And Functions
// -------------------------
MyModule.someData = "public data";
MyModule.someFunction = function(){
console.log(MyModule.someData);
}
});
console.log(MyApp.MyModule.someData); //=> public data
MyApp.MyModule.someFunction(); //=> public data
Very generic, multi-purpose object that can serve many different roles
var MyController = Marionette.Controller.extend({
initialize: function(options){
this.stuff = options.stuff;
},
doStuff: function(){
this.trigger("stuff:done", this.stuff);
},
onClose: function(){
/* ... */
}
});
MyRouter = Marionette.AppRouter.extend({
// "someMethod" must exist at controller.someMethod
appRoutes: {
"some/route": "someMethod"
},
/* standard routes can be mixed with appRoutes/Controllers above */
routes : {
"some/otherRoute" : "someOtherMethod"
},
someOtherMethod : function(){
// do something here.
}
});
myObj = {
someMethod: function(){ /*...*/ }
};
new MyRouter({
controller: myObj
});
Implement messaging bus for build application form decoupled modules
MyApp.vent.on("foo", function(){
console.log("foo event");
});
MyApp.vent.trigger("foo");
MyApp.commands.setHandler("foo", function(){
console.log("the foo command was executed");
});
MyApp.commands.execute("foo");
MyApp.reqres = new Backbone.Wreqr.RequestResponse();
MyApp.reqres.setHandler("foo", function(){
return "foo requested. this is the response";
});
var result = MyApp.reqres.request("foo");
console.log(result);
github.com/dmytroyarmak
dmytroyarmak@gmail.com