📜 ⬆️ ⬇️

From Backbone.js to Marionette.js

Hi, Habr.

This article will discuss what Marionette.js consists of and how to not write your bike.

The article is intended primarily for those who worked with Backbone.js and / or Marionette.js.
For the first acquaintance, the first, overview, part and links at the end of the article will be useful.

First, let's remember what Backbone.js is and what it consists of.
  1. Underscore.js - more than 80 helper functions for convenient, cross-browser work with JavaScript.
  2. Backbone.Events is an implementation of a base event class plus helper functions for associating (bind) events with objects.
  3. Backbone.Model is a data model with access to key / value attributes. It has a download / synchronization mechanism with the server.
  4. Backbone.Collection - collections of models spiced with 28 methods from Underscore.js. Like the models, they have a download / synchronization mechanism with the server.
  5. Backbone.Router and Backbone.history - basic router and page history. Able to detect the coincided path and produce navigation.
  6. Backbone.sync - implementation of loading / synchronizing data through REST using jQuery.ajax.
  7. Backbone.View - view. Out of the box, he can neither render himself nor insert a tree into the DOM.


Part One: Marionette.js


Imagine that we are writing an application on Backbone.js. As a result, we get:
')

The term "piece" can be understood as various architectural implementations of the necessary functions.

At the last BackboneConf conference, the idea was repeatedly voiced that Backbone.js is the basis (skeleton) for writing the framework. Moreover, the basis is quite good.

Marionette.js is one of the libraries that, using all the flexibility of Backbone.js, creates a sketch of the architecture and implements the basis for writing large web applications.

Read more about what Marionette.js consists of (with pictures):


Backbone


Representation


Performance Management


Modules and Application


Tire posts


Rest



Conclusion: do not write your framework, use Marionette.js.

Part Two: Controller / Mediator


I strongly advise you to watch all 9 hours of screenbaster BackboneRails from Brian Mann. Materials contain a huge number of useful pieces, one of which is a detailed description of the use of the controller / mediator.

The basic idea: the life cycle of each view or a logically related group of views (Layout) is controlled by a controller / mediator.
A detailed example: we need to display a list, let it be a list of "Apples".

We are creating a new Apple list controller. The controller, in turn, has the following tasks:

  1. Request the necessary data (from the data provider via the message bus);
  2. Wait for data download, possibly showing a download spinner;
  3. In the case of the Apple list, display a CollectionView or CompositeView;
  4. Listen to events of the displayed view, for example, clicks on the recording of each apple;
  5. If necessary, redirect events to the global message bus;
  6. When closing a view, close yourself;
  7. When you close yourself, close all your subscriptions (bind) to events.

More on the first paragraph

The logic of “how to wait for the data, transferred from the data provider to the controller / mediator, allows us to process this in the most appropriate way for the user interface. For example, in one case we can spin the spinner and after receiving the data display everything, and in the other case, first display the empty representation, and then fill it with the received data.

More on the fifth paragraph

It seemed that one could directly send view events to the global event bus. But the layer in the form of a controller / mediator allows us to use the presentation events to the full and proxy the events that are necessary only for the entire application. Example: for apples, you can use the “delete apple” button in this list and the confirmation dialog (“do you really want to delete this apple?”) As an example; only after these two events processed by the controller / mediator, do we send messages to the global bus that an apple has been removed.

Conclusion: the controller / mediator that implements the mediator pattern (mediator) is excellent for the role of a link of asynchronous data, rendering of views and user actions.

Links and materials:


Source: https://habr.com/ru/post/207730/


All Articles