O'Reilly logo

Getting Started with Backbone Marionette by Arturo Soto, Raymundo Armendariz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Extending Marionette views

A common need while working with Backbone and Marionette and in pretty much every language is to re-use code as much as possible. If you want all your views to behave in a certain way, you can achieve it by extending your Marionette views. In the following example, we will add a log method to all the item views by extending the Marionette.ItemView.

var HandyView = Backbone.Marionette.ItemView.extend({
  initialize:function(){
  Backbone.Marionette.ItemView.prototype.initialize.apply(this,arguments);
  },
  logMessage : function (message){
    console.log(message);
  }
});

Now you just need to start using your HandyView in order to get the benefit of the logMessage function.

 var BookView = HandyView.extend({ alertMessage : function () ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required