alejandroelinformatico.com/slideshows/en/marionette-js/
Alejandro El Informático(@ainformatico) - 2014
Creative Commons by-nc-sa.
Alejandro El Informático, Web developer
Creating webapps in a Composite way. Using Marionette.
Backbone.Marionette is a composite application library for Backbone.js that aims to simplify the construction of large scale JavaScript applications.
model
var Todo = Backbone.Model.extend({
defaults : {
content : 'no content defined'
}
});
collection
var Todos = Backbone.Collection.extend({
model : Todo
});
view
var TodoView = Backbone.View.extend({
events: {
'click .open': 'open',
'click .save': 'save'
},
open : function(e) {
e.prevenDefault();
}
save : function(e) {
e.prevenDefault();
}
initialize: function() {
var _this = this;
_this.listenTo(_this.model, 'change', _this.render);
},
render: function() {
var _this = this;
_this.$el.html(_this.template(_this.model.toJSON()));
return _this;
}
});
view
var TodoView = Marionette.ItemView.extend({
modelEvents : {
'change' : 'render'
},
initialize : function(){
this.listenTo(foo, 'bar', this.bar); //automatic unbind
},
triggers : {
'click .open': 'open',
'click .save': 'save'
},
onOpen : function() {
//actions
}
onSave : function() {
//actions
}
});
ItemView
CollectionView
CompositeView
Region
Layout
Application
Module
Controller
Command
Request
ItemView
#render
automatically passes model propertiestriggers : {}
, prevent by defaultmodelEvents : {}
and collectionEvents : {}
ui : {}
, references /html
elements
var TodoView = Marionette.ItemView.extend({
template : '#tpl-item',
//this.ui.inputElement
ui : {
inputElement : '.edit'
},
collectionEvents: {
'add': 'itemAdded'
},
modelEvents: {
'change:name': 'nameChanged'
},
});
<div id="tpl-item">
<span class="edit"> <%= content %> </span>
</div>
CollectionView
ItemView
#render
s all of the models in the specified collection#render
s an ItemView
emptyView
for empty collections.
var TodoListView = Marionette.CollectionView.extend({
itemView : TodoView,
emptyView : EmptyTodoView,
collection : collection
});
<div id="tpl-empty">
<div class="empty">No data found</div>
</div>
CompositeView
CollectionView
template
var TodoListView = new Marionette.CompositeView({
template : '#tpl-list',
itemView : TodoView,
itemViewContainer : '.list'
});
<div id="tpl-list">
<h1>Title</h1>
<ul class="list"></ul>
</div>
Region
show()
and close()
var region = new Marionette.Region({
el: '#tpl-region',
regions : {
menuRegion : '#menu',
contentRegion : '#content'
}
});
region.menuRegion.show(menuView);
region.contentRegion.show(menuView);
<div id="tpl-region">
<div id="menu"></div>
<div id="content"></div>
</div>
Layout
ItemView
and a collection of Region
objectslayout
sview
s and unbind
s its events
var layout = new Backbone.Marionette.Layout({
template: '#tpl-layout',
regions: {
menu : '#menu',
content : '#content'
}
});
layout.menuRegion.show(menuView);
layout.contentRegion.show(menuView);
<div id="tpl-layout">
<div id="menu"></div>
<div id="content"></div>
</div>
application
region
manager
<body></body>
app = new Marionette.Application();
app.addRegions({
headerRegion : '#header-region',
contentRegion : '#content-region'
});
app.addInitializer(function(){
//init your stuff
});
<body>
<div id="app">
<div id="header-region"></div>
<div id="content-region"></div>
</div>
<!-- templates -->
</body>
module
app.module('Views', function(Views, app){
Views.TodoView = Marionette.ItemView.extend({});
});
new app.Views.TodoView();
controller
var controller = new Marionette.Controller({
initialize: function(options){
//load views, listeners...
}
});
command
app.commands.setHandler('main:show', function(view){
app.mainRegion.show(view);
});
app.execute('main:show', view);
request
app.reqres.setHandler('views:todo', function(options){
return app.Views.TodoView(options || {});
});
var noteView = app.request('views:todo');