JavaScript MVC frameworks like Backbone.js are for adding structure to your front-end.
This is most useful when building [increasingly popular] single-page JavaScript apps (SPJA). If you're heavily using unobtrusive JavaScript, you're probably doing a fair amount of ajax for dynamic content to avoid refreshing the page on the user. SPJA's take this a step further by letting the user visit all areas of an app without ever refreshing the page. Without the structure provided by MVC frameworks, the client-side code can quickly get out of control.
例如,在 Facebook 这样的应用程序中,用户交互事件(例如放大图片时按 L 或单击 Like 按钮)实际上映射到相同的操作,因此该操作应该与构造视图和附加事件的部分解耦处理程序。可以再次将更新的元数据信息实际发送到服务器分离出来,因为这部分可以重新用于更新需要传送回服务器的元数据的任何操作。类似地,元数据的验证可以在更新元数据的不同用户操作之间重复使用。
通过这个例子,我希望能够传达 MVC 设计如何与 Web 前端相结合的概念。
Having a dual MVC is absolutely redundant if your web frontend is simply the presentational layer ie. a view and all your data as well as application logic resides on the server.
However many of the modern complex web apps try to maximize the user experience by creating highly interactive frontends that dynamically communicate with the server using Ajax or similar alternatives eg. Flash AMF. In such cases in the fontend of your application, separating out the script portions that handle actual communication with server, providing proper provisions for managing the local data that has been fetched/cached in the client's system, user interaction event handling and history management. Once you begin to think about it, it quickly becomes apparent that having a separate MVC layer in the javascript code is a good idea because it fits well with the scenario and makes the code manageable.
For instance, in an application like Facebook a user interaction event like pressing L when a picture is enlarged, or clicking on the Like button actually map to the same action and therefore this action should be decoupled from the part that constructs the view and attaches event handlers. Actual sending the updated metadata information to the server can be again separated out because this portion can be re-utilized for any action that updates the metadata which needs to be communicated back to the server. Similarly validation of metadata is reusable across different user actions which update the metadata.
Through this example I hope to have communicated the concept of how MVC design fits in with web frontends.
I think the framework is more towards building complex JS apps. Using ajax to update/save. For example, MVC framework would make it easier to build a js Calculator app. Just my 2cent.
一些应用程序架构师只希望服务器中具有瘦 Web 服务接口的数据层功能。在这种情况下,在客户端中谨慎使用 MVC 架构是很有用的,因为您最终将不得不处理模型层问题,例如验证、复杂视图、数据过滤、控制器层问题(例如视图格式化决策)以及视图层问题,例如布局、渲染和样式。
Some application architects only want data-layer functionality in the server with a thin web services interface. In such a situation, it is useful to have the MVC architecture in the client.
At the end of the day, it depends where you want to put your controller and view functionality. If you want it all in the server use Rails without any AJAX. Having a model layer in the server lets you encode complex relationships, views, and validation in the server. It lets your controller respond to different data formats (e.g. xml, html, json). And it lets you use ERB or HAML to control the views.
However in many situations, you may want to offload processing to the client. In such a case, you may want to move view processing to the client (such as with AJAX). Or you may want to have the client determine whether to render html, xhtml, html5, or whatever. Or you may want to use some local storage to cache data in the client. Or you may want to do data validation and view composition in the browser.
Some application architects only want data-layer functionality in the server with a thin web services interface. In such a situation, it is useful to have the MVC architecture in the client is prudent because you'll eventually have to deal with model-layer issues such as validation, complex views, data filtering, controller-layer issues such as view formatting decisions, and view-layer issues such as layout, rendering, and styling.
But if you start flapping your gums about MVC, node, Angular, backbone, AJAX, RESTful, or JSON...they think you're at the forefront of Computer Science.
It's the same with PHP frameworks. Any CRUD app coder earning $40K/year can write this all day long:
But if you start mentioning MVC, Yii, Laravel, Symfony, or Zend...it's like putting on a tweed jacket with patches on the elbows and holding a coffee cup that says "Professor of the Year".
The same as on any other tie, one needs for JavaScript code: * Abstractions (separation of concerns) * Implicit conventions and consistance * Code reusability
You can get it with Vanilla JS of course, but will have to write a sort of a framework by yourself. So you rather take an existing well-known and proven solution.
These are core requirements, any other depends on how much you allow the framework to restrict your development process.
发布评论
评论(7)
JavaScript MVC 框架(例如 Backbone.js)用于向前端添加结构。
这在构建[日益流行的]单页 JavaScript 应用程序 (SPJA) 时最有用。如果您大量使用不显眼的 JavaScript,您可能会对动态内容执行大量的 ajax 以避免刷新用户的页面。 SPJA 更进一步,让用户无需刷新页面即可访问应用程序的所有区域。如果没有 MVC 框架提供的结构,客户端代码很快就会失控。
JavaScript MVC frameworks like Backbone.js are for adding structure to your front-end.
This is most useful when building [increasingly popular] single-page JavaScript apps (SPJA). If you're heavily using unobtrusive JavaScript, you're probably doing a fair amount of ajax for dynamic content to avoid refreshing the page on the user. SPJA's take this a step further by letting the user visit all areas of an app without ever refreshing the page. Without the structure provided by MVC frameworks, the client-side code can quickly get out of control.
如果您的 Web 前端只是表示层,那么拥有双 MVC 绝对是多余的。视图和所有数据以及应用程序逻辑都驻留在服务器上。
然而,许多现代复杂的 Web 应用程序试图通过创建高度交互的前端来最大化用户体验,这些前端使用 Ajax 或类似的替代方案与服务器动态通信。闪存 AMF。
在这种情况下,在应用程序的前端,分离出处理与服务器的实际通信的脚本部分,为管理已在客户端系统中获取/缓存的本地数据、用户交互事件处理和历史管理提供适当的规定。一旦您开始思考,很快就会发现在 javascript 代码中拥有一个单独的 MVC 层是一个好主意,因为它非常适合场景并使代码易于管理。
例如,在 Facebook 这样的应用程序中,用户交互事件(例如放大图片时按 L 或单击 Like 按钮)实际上映射到相同的操作,因此该操作应该与构造视图和附加事件的部分解耦处理程序。可以再次将更新的元数据信息实际发送到服务器分离出来,因为这部分可以重新用于更新需要传送回服务器的元数据的任何操作。类似地,元数据的验证可以在更新元数据的不同用户操作之间重复使用。
通过这个例子,我希望能够传达 MVC 设计如何与 Web 前端相结合的概念。
Having a dual MVC is absolutely redundant if your web frontend is simply the presentational layer ie. a view and all your data as well as application logic resides on the server.
However many of the modern complex web apps try to maximize the user experience by creating highly interactive frontends that dynamically communicate with the server using Ajax or similar alternatives eg. Flash AMF.
In such cases in the fontend of your application, separating out the script portions that handle actual communication with server, providing proper provisions for managing the local data that has been fetched/cached in the client's system, user interaction event handling and history management. Once you begin to think about it, it quickly becomes apparent that having a separate MVC layer in the javascript code is a good idea because it fits well with the scenario and makes the code manageable.
For instance, in an application like Facebook a user interaction event like pressing L when a picture is enlarged, or clicking on the Like button actually map to the same action and therefore this action should be decoupled from the part that constructs the view and attaches event handlers. Actual sending the updated metadata information to the server can be again separated out because this portion can be re-utilized for any action that updates the metadata which needs to be communicated back to the server. Similarly validation of metadata is reusable across different user actions which update the metadata.
Through this example I hope to have communicated the concept of how MVC design fits in with web frontends.
我认为该框架更倾向于构建复杂的 JS 应用程序。使用ajax更新/保存。例如,MVC 框架可以更轻松地构建 js 计算器应用程序。只是我的2分钱。
I think the framework is more towards building complex JS apps. Using ajax to update/save. For example, MVC framework would make it easier to build a js Calculator app. Just my 2cent.
一些应用程序架构师只希望服务器中具有瘦 Web 服务接口的数据层功能。在这种情况下,在客户端中采用 MVC 架构会很有用。
归根结底,这取决于您想要将控制器和视图功能放置在哪里。如果您希望将所有内容都放在服务器中,请使用 Rails,而不使用任何 AJAX。在服务器中拥有模型层可以让您在服务器中编码复杂的关系、视图和验证。它让您的控制器响应不同的数据格式(例如 xml、html、json)。它允许您使用 ERB 或 HAML 来控制视图。
然而,在许多情况下,您可能希望将处理卸载给客户端。在这种情况下,您可能希望将视图处理移至客户端(例如使用 AJAX)。或者您可能想让客户端确定是否渲染 html、xhtml、html5 或其他内容。或者您可能想使用一些本地存储来缓存客户端中的数据。或者您可能想在浏览器中进行数据验证和视图合成。
一些应用程序架构师只希望服务器中具有瘦 Web 服务接口的数据层功能。在这种情况下,在客户端中谨慎使用 MVC 架构是很有用的,因为您最终将不得不处理模型层问题,例如验证、复杂视图、数据过滤、控制器层问题(例如视图格式化决策)以及视图层问题,例如布局、渲染和样式。
Some application architects only want data-layer functionality in the server with a thin web services interface. In such a situation, it is useful to have the MVC architecture in the client.
At the end of the day, it depends where you want to put your controller and view functionality. If you want it all in the server use Rails without any AJAX. Having a model layer in the server lets you encode complex relationships, views, and validation in the server. It lets your controller respond to different data formats (e.g. xml, html, json). And it lets you use ERB or HAML to control the views.
However in many situations, you may want to offload processing to the client. In such a case, you may want to move view processing to the client (such as with AJAX). Or you may want to have the client determine whether to render html, xhtml, html5, or whatever. Or you may want to use some local storage to cache data in the client. Or you may want to do data validation and view composition in the browser.
Some application architects only want data-layer functionality in the server with a thin web services interface. In such a situation, it is useful to have the MVC architecture in the client is prudent because you'll eventually have to deal with model-layer issues such as validation, complex views, data filtering, controller-layer issues such as view formatting decisions, and view-layer issues such as layout, rendering, and styling.
面试时穿西装吗?同样的事情。
如果 Nobdy 认为前端开发人员整天都在编写这样的代码,那么他们每年将支付 9.5 万美元:
但是如果你开始对 MVC、node、Angular、backbone、AJAX、RESTful 或 JSON 感到惊讶……他们认为您处于计算机科学的前沿。
PHP 框架也是如此。任何年收入 4 万美元的 CRUD 应用程序编码员都可以整天这样写:
但是如果你开始提到 MVC、Yii、Laravel、Symfony 或 Zend……这就像穿上一件肘部贴有补丁的粗花呢夹克并拿着一杯咖啡杯子上写着“年度教授”。
Ever wear a suit to an interview? Same thing.
Nobdy's going to pay a front end developer $95K/year if they think he's coding stuff like this all day long:
But if you start flapping your gums about MVC, node, Angular, backbone, AJAX, RESTful, or JSON...they think you're at the forefront of Computer Science.
It's the same with PHP frameworks. Any CRUD app coder earning $40K/year can write this all day long:
But if you start mentioning MVC, Yii, Laravel, Symfony, or Zend...it's like putting on a tweed jacket with patches on the elbows and holding a coffee cup that says "Professor of the Year".
我们将 Backbone.is 用于 SPA(单页应用程序)。有点像gmail。
We use Backbone.is for a SPA (Single Page App). Kind of like gmail.
与任何其他领带一样,需要 JavaScript 代码:
* 抽象(关注点分离)
* 隐式约定和一致性
* 代码可重用性
当然,您可以使用 Vanilla JS 获得它,但必须自己编写某种框架。因此,您宁愿采用现有的众所周知且经过验证的解决方案。
这些是核心要求,任何其他要求都取决于您允许框架在多大程度上限制您的开发过程。
The same as on any other tie, one needs for JavaScript code:
* Abstractions (separation of concerns)
* Implicit conventions and consistance
* Code reusability
You can get it with Vanilla JS of course, but will have to write a sort of a framework by yourself. So you rather take an existing well-known and proven solution.
These are core requirements, any other depends on how much you allow the framework to restrict your development process.