18

backbone.js has been spoken of as an MVC framework for Javascript. But is it?

Here is the description of the model, from http://documentcloud.github.com/backbone/

Models are the heart of any JavaScript application, containing the interactive data as well as a large part of the logic surrounding it: conversions, validations, computed properties, and access control. You extend Backbone.Model with your domain-specific methods, and Model provides a basic set of functionality for managing changes.

Now, that is not my understanding of the model in MVC at all. In my understanding, the model is just the classes that model the domain, so your Student, School and Teacher objects. The controller does performs the business logic on them and interacts with the view for display and receiving input.

This understanding is consistent with the various definitions I find on the web, IE http://en.wikipedia.org/wiki/Model_view_controller:

Model–View–Controller (MVC) is a design pattern for computer user interfaces that divides an application into three areas of responsibility:

the Model: the domain objects or data structures that represent the application's state.

So, my question is: is backbone.js really a MVC framework in any sense, or is it more of just a general way of helping you to glue everything together?

Incidentally, the definition of the Model from backbone.js's FAQ appears to differ from the one I quoted above (also from backbone.js http://documentcloud.github.com/backbone/#FAQ-mvc:

Backbone.Model – Like a Rails model minus the class methods. Wraps a row of data in business logic.

So in what sense is backbone.js really an MVC or not?

(Caveat: I am just evaluating backbone.js currently.)

mtyson
  • 8,196
  • 16
  • 66
  • 106
  • 1
    That's why many people refer to such frameworks as MV* – Ilia Choly May 24 '12 at 21:52
  • Note the types of *logic* that are indicated as part of the model: "conversions, validations, computed properties, and access control." These are all sensible domain object concerns in any MVC application. They are not the same as the application/business logic, such as routing between views, sending updates to a database, etc. which are usually the responsibility of the controller. – mellamokb May 24 '12 at 21:55
  • 1
    I should really downvote this ( But i ain't doing it) this definetly seems like plot for earn rep!! but anyway nobody said Backbone is MVC did they?? **They explain it as a library that provides structure by using Models, Views, Collections and Routers (again no mention of it's resemblance to MVC)** people can't help with wrong assumptions can they?? – Deeptechtons May 25 '12 at 05:05
  • `So in what sense is backbone.js really an MVC or not?` I know it is an old post but IMHO this question is not constructive and does not fit the SO Q&A format. There is no answer to this question only personal opinion and assumptions. – Nope May 10 '13 at 16:16

6 Answers6

19

Backbone is one of those MV* (MV-star). There is no controller because the logic that operates the app is in the View (something like M(V+C) instead).

And there is no unified definition for "Model" as it is used differently across different frameworks in different languages. But commonly, models are just an abstraction of the data storage and optionally have a bit of logic like validation, formatting and state change hooks.

Joseph
  • 117,725
  • 30
  • 181
  • 234
11

Its a bit confusing when you try to find the "M" (Model) the "V" (View) and the "C" (Controller).

In that way we can always argue on the terms but what's important is the intend.

Intersecting the "Terms" with the "Intend" :

The "M" :

Backbone Models. The Collections are tied to Models.

The "V" :

The default Underscore.js template represents the "View Intention" of standard MVC

The "C" :

Backbone View is the Controller. Its called View in backbone (like in many others like Django) because it handles the rendering. From MVC point of view its a Controller. Routes are tied with the Views

Now, sometimes just to satisfy the MVC abbreviation some call it "Model View Collections". But that is what should not be said.

Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197
2

I think you are getting hung up on the names. Not every MVC framework sticks to the same naming convention. For example, in Django, Controllers are called Views and Views are called Templates. Backbone follows a similar naming pattern. Note, Backbone is very flexible on the views part, not enforcing any particular templating library.

As already noted, the logic included in Backbone models is logic that is the concern of the model. A standard relational database, which is a collection of models (tables), typically cares about the type of data in columns.

Logic that would typically go in your controllers (Backbone's "Views") would be things like handling transient session data, checking user state (authenticated for example, although you wouldn't be doing that in js), binding views to models (Backbone does exactly this, managing where and when models are stored/created based on user actions and binding templates to update on model changes).

So, in summary, Backbone actually sticks pretty well to MVC, much better than most server side "MVC" frameworks which are typically really "PAC" frameworks (Presentation-Abstraction-Control).

Endophage
  • 21,038
  • 13
  • 59
  • 90
2

Kind of both. If you're looking at the strict sense of a model; that is, that the model is simply data, then no. But validations, conversions, etc., have been placed there because Backbone is fairly model-centric. From my own experience, I typically treat Backbone models in the traditional sense in that the model is an application's representation of the backend data store, and all it has are accessor methods to manipulate that data.

The point is that though the facilities to validate or perform access control, etc. exist, you can choose to follow a more classical MVC pattern. It's entirely up to you. That's what I love about Backbone is that it gives you an MVC engine on which you build an entire application architecture. If you want to follow the classical MVC design pattern, you can certainly do that. Cheers!

Brendan Delumpa
  • 1,155
  • 1
  • 6
  • 11
1

in backbone.JS MVC stands for MODEL VIEW COLLECTION...

Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
1

If you seek controllers in Backbone.js, you probably want to take a look at this: http://chance-an.github.com/backbone-mvc/

WawaBrother
  • 2,195
  • 3
  • 14
  • 15