Backbone.Marionette

4.1.3 · maintenance · verified Sun Apr 19

Backbone.Marionette, currently at stable version 4.1.3, is a composite application library designed to bring architectural patterns, view management, and memory management to applications built with Backbone.js. It provides a structured approach to developing large-scale single-page applications by offering components like `Application`, `Region`, `View`, and `CollectionView`. While Backbone offers core building blocks, Marionette extends these with sensible defaults, an event-driven architecture via Backbone.Radio, and built-in lifecycle management, including "zombie-killing" for views. This specific package (`backbone.marionette`) is currently in maintenance mode, with its development limited to bug fixes. All new feature work for the framework has transitioned to a new, dependency-agnostic `marionette` package (v5+), which no longer relies on Backbone. This `backbone.marionette` package requires Backbone v1.3.3+ and Underscore v1.8.3+ as peer dependencies, ensuring its compatibility within the Backbone ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic Marionette Application, define a Region, instantiate an ItemView with a simple template, and display it within the application's region. It also shows how to update the view dynamically.

import { Application, Region, ItemView } from 'backbone.marionette';
import Backbone from 'backbone';
import _ from 'underscore';

// 1. Create a simple ItemView to display content
const MyItemView = ItemView.extend({
  template: _.template('<h1>Hello, <%= name %>!</h1><p>This is a Marionette ItemView.</p>'),
  className: 'my-item-view bg-blue-100 p-4 rounded-md shadow-sm',

  initialize(options) {
    this.model = new Backbone.Model({ name: options.name || 'World' });
    console.log('ItemView initialized for:', this.model.get('name'));
  },

  onRender() {
    console.log('MyItemView rendered with name:', this.model.get('name'));
  }
});

// 2. Create a Marionette Application instance
const MyApp = Application.extend({
  // Define a default region for the application to render into
  region: {
    el: '#app-hook',
    replaceElement: true // Replace the target element with the application's view
  },

  onStart() {
    console.log('Marionette Application started!');
    // Show an initial view in the main region
    this.showView(new MyItemView({ name: 'Marionette User' }));

    // Demonstrate updating the view after a short delay
    setTimeout(() => {
      console.log('Updating view after 2 seconds...');
      this.showView(new MyItemView({ name: 'Updated User' }));
    }, 2000);
  },

  // Application-level events can be handled here
  onBeforeStart() {
    console.log('Application is about to start...');
  }
});

// 3. Ensure the DOM element exists before starting the app
document.addEventListener('DOMContentLoaded', () => {
  const appContainer = document.createElement('div');
  appContainer.id = 'app-hook';
  document.body.appendChild(appContainer);

  // 4. Instantiate and start the application
  const myApp = new MyApp();
  myApp.start();
});

view raw JSON →