javascript Backbone.js 真的是 MVC 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10745782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is Backbone.js really an MVC?
提问by mtyson
backbone.js has been spoken of as an MVC framework for Javascript. But is it?
Backbone.js 被称为 Javascript 的 MVC 框架。但是是吗?
Here is the description of the model, from http://documentcloud.github.com/backbone/
这是模型的描述,来自http://documentcloud.github.com/backbone/
Modelsare 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.
模型是任何 JavaScript 应用程序的核心,包含交互式数据以及围绕它的大部分逻辑:转换、验证、计算属性和访问控制。您可以使用特定于域的方法扩展 Backbone.Model,而 Model 提供了一组用于管理更改的基本功能。
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.
现在,这根本不是我对 MVC 模型的理解。在我的理解中,模型只是为域建模的类,所以你的学生、学校和教师对象。控制器确实对它们执行业务逻辑,并与视图交互以显示和接收输入。
This understanding is consistent with the various definitions I find on the web, IE http://en.wikipedia.org/wiki/Model_view_controller:
这种理解与我在网上找到的各种定义一致,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.
模型-视图-控制器 (MVC)是一种计算机用户界面设计模式,它将应用程序划分为三个职责范围:
模型:代表应用程序状态的域对象或数据结构。
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?
所以,我的问题是:从某种意义上说,backbone.js 真的是一个 MVC 框架,还是只是一种帮助您将所有东西粘合在一起的通用方法?
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.js 的 FAQ 中模型的定义似乎与我上面引用的不同(也来自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.
Backbone.Model– 就像 Rails 模型减去类方法。在业务逻辑中包装一行数据。
So in what sense is backbone.js really an MVC or not?
那么从什么意义上说,backbone.js 真的是一个 MVC 吗?
(Caveat: I am just evaluating backbone.js currently.)
(警告:我目前只是在评估backbone.js。)
采纳答案by Yugal Jindle
Its a bit confusingwhen you try to find the "M" (Model) the "V" (View) and the "C" (Controller).
当您试图找到“ M”(模型)、“ V”(视图)和“ C”(控制器)时,它有点令人困惑。
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 Collectionsare tied to Models.
骨干模型。该集合被绑定到模型。
The "V" :
“V”:
The default Underscore.jstemplate represents the "View Intention" of standard MVC
默认的Underscore.js模板代表标准MVC的“视图意图”
The "C" :
“C”:
Backbone Viewis the Controller. Its called Viewin backbone (like in many others like Django) because it handles the rendering. From MVCpoint of view its a Controller. Routesare tied with the Views
主干视图是控制器。它在主干中称为View(就像在许多其他人中一样,如Django),因为它处理渲染。从MVC 的角度来看,它是一个控制器。路线与视图相关
Now, sometimes just to satisfy the MVCabbreviation some call it "Model View Collections". But that is what should not be said.
现在,有时只是为了满足MVC 的缩写,有人称其为“模型视图集合”。但那是不应该说的。
回答by Joseph
Backbone is one of those MV* (MV-star). There is no controllerbecause the logic that operates the app is in the View (something like M(V+C) instead).
Backbone 是那些 MV*(MV-star)之一。有没有控制器因为操作该应用程序中的逻辑是在视图(像M(V + C)代替)。
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.
并且“模型”没有统一的定义,因为它在不同语言的不同框架中的使用方式不同。但通常,模型只是数据存储的抽象,并且可选地具有一些逻辑,例如验证、格式化和状态更改挂钩。
回答by Endophage
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.
我想你对这些名字很着迷。并非每个 MVC 框架都遵循相同的命名约定。例如,在 Django 中,控制器称为视图,视图称为模板。Backbone 遵循类似的命名模式。注意,Backbone 在视图部分非常灵活,不强制执行任何特定的模板库。
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.
如前所述,Backbone 模型中包含的逻辑是模型关注的逻辑。标准关系数据库是模型(表)的集合,通常关心列中的数据类型。
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).
通常会出现在您的控制器中的逻辑(Backbone 的“视图”)将是诸如处理瞬态会话数据、检查用户状态(例如经过身份验证,尽管您不会在 js 中这样做)、将视图绑定到模型(Backbone 可以)正是这一点,根据用户操作和绑定模板来管理存储/创建模型的位置和时间,以更新模型更改)。
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).
因此,总而言之,Backbone 实际上非常适合 MVC,比大多数服务器端“MVC”框架要好得多,后者通常是真正的“PAC”框架(演示-抽象-控制)。
回答by Brendan Delumpa
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.
两种都有。如果您正在查看模型的严格意义;也就是说,模型只是数据,然后不是。但是验证、转换等都放在那里,因为 Backbone 是相当以模型为中心的。根据我自己的经验,我通常在传统意义上对待 Backbone 模型,因为该模型是应用程序对后端数据存储的表示,它所拥有的只是操作该数据的访问器方法。
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!
关键是虽然存在验证或执行访问控制等的工具,但您可以选择遵循更经典的 MVC 模式。这完全取决于你。我喜欢 Backbone 的原因在于它为您提供了一个 MVC 引擎,您可以在该引擎上构建整个应用程序架构。如果您想遵循经典的 MVC 设计模式,您当然可以这样做。干杯!
回答by Parv Sharma
in backbone.JS MVCstands for MODEL VIEW COLLECTION...
在backbone.JS MVC代表模型视图集合......
回答by WawaBrother
If you seek controllers in Backbone.js, you probably want to take a look at this: http://chance-an.github.com/backbone-mvc/
如果你在 Backbone.js 中寻找控制器,你可能想看看这个:http: //chance-an.github.com/backbone-mvc/