Charles
Charles

Reputation: 309

When to use a Backbone.View vs. Backbone.Marionette.ItemView?

I'm working my way through my first attempt at using Backbone.Marionette and wondered if there is any reason to use a Backbone.Marionette.ItemView when a simple Backbone.View will suffice?

Thanks!

Upvotes: 5

Views: 1596

Answers (1)

brettjonesdev
brettjonesdev

Reputation: 2281

Marionette's ItemView provides a default render and close method which do a few things for you.

render does the following (amongst other things):

1) Marks view as !this.isClosed (related to showing views in Regions, another Marionette concept)

2) Triggers before and after render events

3) Calls a serializeData method to get data (defaults to data from either this.model or this.collection.

4) Gets the template (possibly from TemplateCache) and renders via Marionetter.Renderer

5) Binds UI elements

In addition to this "free" render method, you also get close functionality.

Of course, if you are using a CollectionView/ItemView combination (which is very powerful) or a CompositeView/ItemView, you HAVE to use an ItemView.

Outside of those cases, one big benefit is that you KNOW your view is going to be compatible with Marionette Regions. Ideally, in a Marionette app, you use separate regions to show your views, like so:

var myView = new MyView();

// render and display the view
MyApp.mainRegion.show(myView);

// closes the current view
MyApp.mainRegion.close();

If MyView is a Marionette ItemView, this will work like a charm. If it is just a raw Backbone View, you will probably need to do some work to make sure that it works as planned.

In my apps, I typically just opt for using ItemView as the basis for all of my non-Collection and non-Composite Views.

Upvotes: 16

Related Questions