
What's new in Marionette.js 3.0?

More than 2 years have passed since the 3rd version began to be developed and finally it was released today! So, who cares, who was waiting and working with Marionette.js - welcome to the tackle.
The guys from the core team did a very good job and added a couple of good changes.
Let's start.
View
Marionette.View has noticeably transformed. Now this is not just an unused class, as we remember from the version 2 documentation
Note: The Marionette.View class is not intended to be used directly
and a full View.
Moreover, it now contains both Marionette.View
, and, Marionette.ItemView
and Marionette.LayoutView
. Yes, as you understand now we no longer have Marionette.ItemView
neither Marionette.LayoutView
, they were deleted. To use the third version of the code you need to replace your only Marionette.ItemView
and Marionette.LayoutView
at Marionette.View
all.
Let's look at a small example:
import Mn from 'backbone.marionette';
const MyView = Mn.View.extend({
template: _.template('Превый заголовок для 3-й версии
'),
onRender() {
console.log('Моя вью была отрендерена')
}
});
And an example of how to use View
asLayoutView
import Mn from 'backbone.marionette';
const MyView = Mn.View.extend({
regions: {
region1:'#region1',
region2: '#region2'
},
onRender() {
this.showChildView('region1', childView1);
this.showChildView('region2', childView2);
}
});
We figured it out. Move on.
Compositeview
Marionette.CompositeView has become deprecated
what causes many questions at once. How do I now create a tablet or tree menu as recommended in the documentation? Everything is very simple, for this you need to use Marionette.View
and Marionette.CollectionView
. The guys prepared good comparative examples for both tables and tree menus .
By the way, the documentation for the new version has improved markedly. Scott Walton did a good job of it, aka Marionette Guides .
Collectionview
Marionette.CollectionView remained basically unchanged. Methods getChildView
and getEmptyView
have been deleted. Instead, you can do so
Mn.CollectionView({
childView() {
// мой код
},
emptyView() {
// мой код
}
});
Also Backbone.BabySitter
removed from dependencies and fully integrated into the core of the framework. Now let's just refresh the memory with a small example.
import Mn from 'backbone.marionette';
const data = [
{
item: 'Превая запись'
},
{
item: 'Вторая запись'
},
{
item: 'Третья запись'
}
];
const collection = new Backbone.Collection(data);
const childViewTemplate = _.template('<%= item %>');
const ChildView = Mn.View.extend({
template: childViewTemplate
});
const collectionView = new Mn.CollectionView({
el: 'body',
childView: ChildView,
collection: collection
});
collectionView.render();
View.Events
The life cycle of View has changed. Events before:show
and show
have been deleted. Now it looks like this:
before:render -> render -> before:attach -> attach -> dome:refresh
before:detach -> detach -> before:destroy -> destroy
Example
import Mn from 'backbone.marionette';
const MyView = Mn.View.extend({
template: false,
onBeforeRender() {
console.log(1)
},
onRender() {
console.log(2)
},
onbeforeDestroy() {
console.log(3)
},
onDestroy() {
console.log(4);
}
});
const myView = new MyView();
myView.render();
myView.destroy();
Childview events
The API has changed a bit and now childEvents
needs to be used instead childViewEvents
.
import Mn from 'backbone.marionette';
const MyView = Mn.View.extend({
childViewEvents: {
'some:event': 'eventHandler'
}
eventHandler() {
console.log('Событие дочернего елемента');
}
});
Templates
templateHelpers
was renamed to templateContext
.
import Mn from 'backbone.marionette';
const MyView = Mn.View.extend({
template: template,
templateContext() {
return {
version: '3.0'
}
}
});
Backbone.Radio
Replaced Backbone.Wreqr
by Backbone.Radio - a powerful library for communication between modules in the application. Backbone.Radio
tightly integrated into Marionette.Object
which makes it possible to listen to all the events of the application in one place.
API changes
- `bindEntityEvents` -> `bindEvents`
- `unbindEntityEvents ` -> `unbindEvents`
- `normalizeUIString`, `normalizeUIKeys`, `normalizeUIValues` -> `normalizeMethods`
- `proxyGetOption` -> `getOption`
- `proxyBindEntityEvents` -> `bindEvents`
- `proxyUnbindEntityEvents` -> `unbindEvents`
What was deleted?
Marionette.Controller
Marionette.Module
Marionette.RegionManager
Переезд на новую версию должен быть не очень болезненным.
Вот коммиты одного из лидеров кор команды Paul Falgout в один из своих проектов:
templateHelpers -> templateContext
Marionette.ItemView -> Marionette.View
Marionette.LayoutView -> Marionette.View
childEvents -> childViewEvents
render:collection / onRenderCollection -> render:children / onRenderChildren
before:show / show / onBeforeShow / onShow -> attach etc
Чтобы облегчить жизнь разработчикам, была создана баблиотека marionette-v3-compat.
Так же есть примеры третьей версии с разными сборщиками проектов.
Только зарегистрированные пользователи могут участвовать в опросе. Войдите, пожалуйста.
Какую библиотеку Вы используете для фронтенд приложений?
- 18.3%Marionette.js110
- 24.8%Backbone.js149
- 29%Angular.js174
- 4.5%Ember.js27
- 32.2%React.js193
- 10.1% Vue.js 61
- 10% Another option 60
- 14.3% I do not use frameworks 86