AngularJS客户端MVC模式? [英] AngularJS client MVC pattern?

查看:324
本文介绍了AngularJS客户端MVC模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到现在为止,我主要是利用的Struts 2 JQuery的技术堆栈构建Web应用程序。问题的关键是,所提到栈使用服务器端的 MVC 模式。 Web浏览器的主要作用是有限的请求/响应周期(+客户端验证)。数据检索,业务逻辑,布线和验证,主要是在服务器端的职责。

Until now I was mainly using Struts 2, Spring, JQuery technology stack for building web applications. The point is, that mentioned stack uses server side MVC pattern. The main role of web browsers was limited to a request/response cycle (+ client side validation). Data retrieval, business logic, wiring and validation were mainly responsibilities of the server side.

我有一个关于几个问题的 AngularJS 的框架,它是由以下引号我读过的启发:

I have few questions regarding AngularJS framework that were inspired by following quotes I've read:

AngularJS教程

有关角的应用程序,我们鼓励使用模型 - 视图 - 控制器
  (MVC)设计模式来解耦code和分离的关注。

For Angular apps, we encourage the use of the Model-View-Controller (MVC) design pattern to decouple the code and to separate concerns.

维基百科模型 - 视图 - 控制器 的:

模型 - 视图 - 控制器(MVC)是分割的体系结构
  再从用户与交互信息presentation
  它。该模型包括应用程序数据和业务规则的,
  和控制器介导输入,将其转换为命令
  模型或视图

Model–View–Controller (MVC) is an architecture that separates the representation of information from the user's interaction with it. The model consists of application data and business rules, and the controller mediates input, converting it to commands for the model or view

AngularJS 的使用客户端 MVC 模式。所以我想有没有其他选项,然后包括验证逻辑也以某种方式在客户端?


AngularJS uses client side MVC pattern. So I guess there is no other option then to include validation logic also to the client side in some way?

什么是写一个强大的应用程序AngularJS的最佳方式? MVC在客户端和服务器端的某种MC(模型,控制器)?

这是否意味着,模型和控制器都在重复一种方式(客户端/服务器)?

Does that means, that MODEL and CONTROLLER are in one way duplicated (client/server)?

我知道我的问题是有点怪异,但我认为原因是,我莫名其妙地acustomed传统的服务器端MVC模式。我相信有一个人,是已经做了相同的过渡。

I know my question is somehow weird, but I think the reason is, that I am somehow acustomed to traditional server side MVC pattern. I am sure there is someone, that have already done same transition.

推荐答案

别客气一个奇怪的问题 - 我一直在试图角出售给了很多的Java开发者,他们问这个。我问我自己,当我学习(我还在学习,顺便说一句)

Not at all a weird question - I've been trying to sell Angular to a lot of java developers and they ask this. I asked it myself when I was learning (I'm still learning, btw)

如果你把一个传统的Java web应用程序如你所描述和角IZE它,你得先您的服务器,使其一个RESTful API。取出的JSP等,这其实是最困难的部分,国际海事组织,写一个角度应用程序 - 获得REST API的权利。对我来说,决定进入服务器需要什么逻辑的关键在于想到它是一个纯粹的API和遗忘的时刻,这将有一个前端

If you take a 'conventional' java webapp as you've described and Angular-ize it, you've got to first take your server and make it a RESTful API. Remove the JSPs, etc. This is actually the hard part, IMO, of writing an Angular app - getting the REST API right. The key for me to deciding what logic needed to go into the server was thinking of it as a pure api and forgetting for the moment that it will have a front end.

这问题真的帮了我 - 如果有人试图挽救一个给定的资源和资源没有有效的数据没有前端,告诉他们 - 他们直接击中API让API需要拒绝。这样,后端负责深验证。这适用于您的业务逻辑。假设有人正在使用的只是的API并它将成为清楚你的服务器需要做的。

That question really helped me - if someone tries to save a given resource and that resource doesn't have valid data there's no front end to tell them - they're hitting the API directly so the API needs to reject it. So, the back end is responsible for the deep validation. This applies to your business logic as well. Assume someone is using just the API and it will become clear what your server needs to do.

服务器需要也鬻数据(可能)JSON格式(我用的Spring MVC +杰克逊),所以它负责公开模型角,沟通与数据库。

The server needs also to vend data in (probably) json format (I use Spring MVC + Jackson), so it's responsible for exposing the model to Angular, and communication with the database.

那么什么是MVC然后在角边?

So what's the MVC then on the Angular side?


  • 型号:即来自REST API的数据。如果API被贩卖JSON,那么这些对象将已经是第一类JavaScript对象。

  • 查看:HTML,当你需要操作DOM指令

  • 控制器:(而且你已经排除你的控制器的定制服务。)

    • 查询REST API,并提出什么是必要的视图上的$范围

    • 提供的回调指示对事件则可能需要回调到服务器响应。

    • 验证:通常通过回调到一个指令。的可能会有些重叠,你已经把在服务器的验证,但你不想让你的用户在等待服务器来验证一切 - 客户应该知道的的东西有关验证给用户的即时反馈。

    • 业务逻辑:pretty大致相同的故事,验证

    • Model: The data that comes from the REST API. If the API is vending JSON, then these objects will already be 1st class javascript objects.
    • View: HTML, and directives when you need to manipulate the DOM
    • Controller: (and custom services that you've factored out of your controllers..)
      • Queries the REST API and puts what's necessary for the View on the $scope
      • Provides callbacks for directives to respond to events that might then require calls back to the server.
      • Validation: usually via a callback to a directive. Will likely overlap some of the validation you've already put in the server, but you don't want your user to wait for the server to validate everything - the client should know something about the validation to give the user immediate feedback.
      • Business logic: Pretty much the same story as validation.

      但是为什么逻辑在客户端和服务器中的重复?这主要是因为你没有写一个应用程序,你写两个独立的事情:

      But why the duplication of logic in the client and in the server? Mostly because you're not writing one app, you're writing two independent things:


      1. 一个REST API,需要将没有了前端
      2. 稳健且可用
      3. ,需要得到即时反馈给用户,并且不一定等待一个服务器的GUI。

      所以,简单的答案 - 向右忘记了会有一个UI,并且什么进入角将更加清晰拿到REST API

      So, short answer - get the REST API right by forgetting that there will be a UI, and what goes into Angular will be much clearer.

      这篇关于AngularJS客户端MVC模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆