backbone.js是mvc架构吗?

参考1:http://stackoverflow.com/questions/10745782/is-backbone-js-really-an-mvc

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, IEhttp://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.)

share | improve this question

 
That's why many people refer to such frameworks as MV* –  ilia choly  May 24 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 at 21:55
 
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 at 5:05
feedback

6 Answers

up vote 2 down vote accepted

Checkout this article by Derick Bailey called BackboneJS is NOT an MVC Framework.

He does a really good job explaining it.

http://lostechies.com/derickbailey/2011/12/23/backbone-js-is-not-an-mvc-framework/

share | improve this answer
 
Nice, that's it exactly. The MVC designation is useful for learning, but inaccurate. In this case, MVC really just means 'A general framework for glueing stuff together, and has some parts that are similar to an MVC.' Thx for article link. –  mtyson  May 25 at 18:22
 
No problem! Glad to help... –  jcreamer898  May 25 at 18:27
feedback

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.

share | improve this answer
 
feedback

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!

share | improve this answer
 
feedback

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).

share | improve this answer
 
feedback

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

share | improve this answer
 
feedback

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


参考资料2:Backbone.js Is Not An MVC Framework

http://lostechies.com/derickbailey/2011/12/23/backbone-js-is-not-an-mvc-framework/

Backbone.js Is Not An MVC Framework

I’ve seen this question / statement / argument more than a few dozen times. I don’t particularly care whether or not people try to understand Backbone in terms of MVC frameworks, because that’s how we learn. We adapt new ideas based on existing knowledge experience, before we fully understand the new idea. However, I do care that people continue to say Backbone is an MVC framework, because it confuses people when they don’t see a “Controller” and see things like “Routers/Views are sort of a controller“.

I’m not going to “set the record straight” or try to offer the definitive answer on this, though. I’m not a core contributor to Backbone. I didn’t build it and I don’t have any say in how it’s truly architected and designed. So, none of what I say is official. I am going to offer my opinion, though. I feel my years of experience in working with MV* family applications at least gives me enough knowledge to do that… which, honestly, is only going to muddy the waters more because I seem to be in the minority on this opinion.

There Are No Controllers In Backbone

There simply aren’t. In spite of the documentation saying that routers or view may be sort of maybe almost kind of close to some of what a controller might do, there are no controllers. It’s not a construct that exists in the backbone namespace, and there’s no implementation that represents what a controller does, architecturally.

A router is not a controller. It’s a router. A view is not a controller. It’s a view. Yes, both routers and views share some of what a modern web-server MVC framework, such as Ruby on Rails, would call a controller. But neither of these is a controller.

More MVP Than MVC

I’ve spent 5+ years building MV* family applications in thick-client / GUI systems (Windows / WinForms / WinMobile) and on the web (Rails, ASP.NET, ASP.NET MVC, etc). Backbone clearly fits in to this family, but it’s also clearly not MVC. My opinion says that it’s closer to MVP, where the backbone view is closer to a P (presenter) and the HTML/DOM is the V (view).

Consider this picture of an MVC process flow (from Wikipedia):

NewImage

Here, the models contain data which is used to populate views. Actions that a user initiates are handled by the controller which processes the request and updates the models. The models are then fed back to the views and the cycle starts over. It’s cyclical in nature.

And now consider this picture of an MVP process flow (from LessThanDot.com):

NewImage

Notice the difference? Right – it’s not circular. That’s the big difference between MVC and plain-jane MVP (a.k.a. “passive view“). MVP does not work in a circular fashion the way MVC does. Instead, it relies on a presenter (the “P” in MVP) to be the coordinating brains of the operation. The presenter in an MVP app is responsible for taking the data from the models and stuffing it in to the views. Then when an action is taken on the view, the presenter intercepts it and coordinates the work with the other services, resulting in changes to models. The presenter then takes those model changes and pushes them back out to the view, and the elevator of moving data up and down the architectural stack begins again.

Does the idea and responsibility of a presenter sound familiar when thinking about Backbone? It should… it fits almost concept for concept with Backbone’s views, in my mind. But that doesn’t mean Backbone is an MVP implementation, either. It only means that views should be thought of as presenters, not controllers.

Neither MVP, Nor MVC, Nor MVVM, Nor …

Ultimately, trying to cram Backbone in to a pattern language that doesn’t fit is a bad idea. We end up in useless arguments and overblown, wordy blog posts like this one, because no one can seem to agree on which cookie-cutter pattern name something fits in to.

Backbone, in my opinion, is not MVC. It’s also not MVP, nor is it MVVM (like Knockout.js) or any other specific, well-known name. It takes bits and pieces from different flavors of the MV* family and it creates a very flexible library of tools that we can use to create amazing websites.

So, I say we toss MVC/MVP/MVVM out the window and just call it part of the MV* family. Or better yet, let’s just call it “The Backbone Way” and forget about trying to fit some cookie cutter mold around a fluid and flexible library.Then we can focus on what Backbone actually provides and how these pieces should work together to create our apps.

About Derick Bailey

Derick Bailey is an independent consultant, software developer, writer, blogger, speaker and technology leader in central Texas (north of Austin). He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey@mutedsolutions@backbonejsclass Find me on the web:  MutedSolutions.comBackboneTraining.netWatchMeCode
This entry was posted in  BackboneDesign PatternsJavascriptModel-View-ControllerModel-View-Presenter. Bookmark the  permalink. Follow any comments here with the  RSS feed for this post.
  • http://twitter.com/demisbellot Demis Bellot

    >>More MVP Than MVC

    I don’t agree.

    I believe Backbone more closely resembles the original MVC GUI paradigm as existed before MVC web frameworks changed its meaning:http://martinfowler.com/eaaDev/uiArchs.html#ModelViewController

    • http://mutedsolutions.com Derick Bailey

      Like I said: bits and pieces of many flavors of MV*, not MVP or MVC, my opinion, and useless discussions that don’t get us anywhere. The point of the article is at the bottom.

    • http://mutedsolutions.com Derick Bailey

      and FWIW: I agree with you. I wasn’t considering the old-school MVC, though, as that’s not what people refer to when they say MVC, in general. I usually hear people say “SmallTalk MVC” when they refer to that.

  • Lance Harper

    What about Spine.js?

    • http://mutedsolutions.com Derick Bailey

      I don’t have enough experience with spine to say much about it. I’ve only looked through the docs and played with a few samples, a few times.

    • Blake

      In spine, the controller is similar to the view in backbone. Collections and models are consolidated and instantiated/managed on a more global scale. Other than that the patterns are the almost the same. Controller creates views and subscribes to changes on models in order to update.

  • Mike

    I propose a new acronym MVS, Model-View-Something

  • http://twitter.com/stevemgentile Steve Gentile

    Just a thought, but because of no controller or presenter, we end up needing things like Marionette to provide view switching, etc…?

    IMO, I view that part of Backbone ‘out of the box’ as being incomplete.  Although you could say he just left it open for others to implement the controller/presenter/viewmanager.  Once your app starts to go down that composite route we are in need of these ‘missing’ pieces

    • http://mutedsolutions.com Derick Bailey

      Yeah, I think that’s part of the downside of open and flexible libraries like Backbone. If Backbone provided an application layer it would likely become more prescriptive in how you have to do things… like Sproutcore or Batman.js. There are plenty of benefits, but also some drawbacks, to both situations. Open and flexible, but less guidance and a few missing pieces… or more prescriptive, but with all the pieces right there in the framework for you.

  • gabriel

    Change Backbone.View to Backbone.Controller. What do you think?

    • http://mutedsolutions.com Derick Bailey

      No… calling an orange a hammer doesn’t mean you can pound a nail into wood with an orange.

      I could get behind renaming it to Backbone.Presenter, for the reasons I’ve stated above, though.

      • http://www.facebook.com/ron.reiter Ron Reiter

        Derick, Backbone.View IS AN MVC CONTROLLER, it’s just a poor choice of words. The view in HTML models is the HTML and CSS which the Backbone.View (the Controller) handles!

        Read my presentation to see an image which explains the relations:http://www.slideshare.net/ronreiter/writing-html5-web-apps-using-backbonejs-and-gae/
        TBH, it’s quite simple once you accept the fact that Backbone.View is a controller. BTW, check out Android programming, they do something really similar – there, the Backbone.View is called an adapter.

        • http://twitter.com/plapinhh p l

          Why must one accept something doesn’t like to be this??? ;-)

          Controller in MVC (we are talking about its original in Smalltalk-80, aren’t we?) didn’t have some functions the BB.View have. The View was more active about data fetching, its state holding and manipulating itself. All this we don’t have in BB. The View ist simple markup without logic. All logic about presentation is bound to BB.View. So I can see BB implementing MVP and brings some nice features for data layer with.  

        • http://mattbriggs.net Matt Briggs

          In MVC, a view wires itself to the controller, and changes as the controller changes. The controller has no reference to the view. In MVP, the presenter reaches into the view and messes with it, and can also talk to other presenters.

          I don’t think these discussions are super important, but I would highly recommend reading http://martinfowler.com/eaaDev/uiArchs.html

  • Luc Perkins

    I argue here that Backbone does have an implicit and somewhat creepy deus ex machina controller:

    http://blog.appfog.com/putting-some-mvc-meat-into-your-app-backbone-js/ 

  • Chance

    Here is the controller for Backbone.js : http://chance-an.github.com/backbone-mvc/

  • Vlad

    Hi Derick!
    I have 1 small question/suggestion. When you say

    “A router is not a controller. It’s a router. A view is not a controller.
    It’s a view. Yes, both routers and controllers share some of what a
    traditional MVC framework would call a controller”

    I think the 5th sentence should be corrected to “yes, both routers and VIEWS share some…”.

    Or did you really mean “controller” as old name of router in Backbone?

    • http://mutedsolutions.com Derick Bailey

      Yes, you’re correct. I meant to say “views”. Fixed. Thanks! :)

      • Vlad

        cool :)

  • Guest

    By now the term MVW (Model-View-Whatever) has already been coined and the carrot that is isomorphic javascript, or JS that runs both on the client and server, is furiously being sought after. Will Flatiron the first one to the finish line?

    http://blog.nodejitsu.com/scaling-isomorphic-javascript-code

  • Guest

    By now the term MVW (Model-View-Whatever) has already been coined and the carrot that is isomorphic javascript, or JS that runs both on the client and server, is furiously being sought after. Will Flatiron be the first one to the finish line?

    http://blog.nodejitsu.com/scaling-isomorphic-javascript-code

  • http://www.habdas.org/ Josh Habdas

    By now the MVW (Model-View-Whatever) has been coined. More patterns have emerged and the carrot, still dangling, seems to be isomorphic javascript:

    http://blog.nodejitsu.com/scaling-isomorphic-javascript-code

  • http://twitter.com/brokenseal Davide Callegari

    Hi,

    I know this is an old post but I found it during my googling and I’d like to give my thoughts on it.

    I find Backbone appealing because I come from a Django background and I find it very similar to it, since Django has its own router (a urlpattern tuple), a set of templates, views that are responsible for rendering a template and models.

    Django refers to itself not strictly an MVC but MTV (Model-Template-View), which is a bit different but from the same “family” of frameworks.

参考资料3: BabackboneMVC
http://chance-an.github.com/backbone-mvc/#root/index

BackboneMVC adds the missing Controller component to your project if you use Backbone.js, giving you a complete MVC framework. Like CakePHP, it automatically routes your urls to the correct controller and action, but it is a framework for the web front end.

Introduction

Backbone.js does an excellent job of keeping the HTML UI and your Javascript business in sync. However the difficulties we face when we build Javascript web applications are not completely addressed by Backbone.js. Sometimes the difficulty of efficiently organizing the Javascript logic itself is prominent. Using urls to organize the execution of the Javascript logic is a good way to implement your business logic. The browsers' assumption of a traditional server/client model is not compromised, so the browsers' basic functions can be preserved. It also helps resume the correct stage in your business flow from a stateless page.

Backbone.js provides the Router component to map a url to a function. It becomes cumbersome when the application is divided into many modules and there are many operations to be mapped for different urls. Besides, due to the limitations of the language itself, it is not easy to share common processes (like pre-processors and post-processors) among those operations without a lot of code duplication. It will become even verbose if your application has a lot asynchronous calls.

BackboneMVC is designed to address just these issues.

Now you don't have to worry about defining the Router. BackboneMVC takes care of this chore for you automatically. Just define the Controllers and it starts to work automatically. You also get the functionality of automatic session checking and event hooks like 'beforeFilter' and 'afterRender'. There are also some Backbone.js helper utilities provided as well.

BackboneMVC doesn't accomplish what Backbone cannot achieve, but it makes many tasks easier by providing useful shortcuts. So it is very practical and useful.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值