简体   繁体   English

单页JavaScript Web应用程序的体系结构?

[英]Architecture of a single-page JavaScript web application?

How should a complex single-page JS web application be structured on the client-side? 如何在客户端构建复杂的单页JS Web应用程序? Specifically I'm curious about how to cleanly structure the application in terms of its model objects, UI components, any controllers, and objects handling server persistence. 具体来说,我很好奇如何根据模型对象,UI组件,任何控制器和处理服务器持久性的对象来干净地构建应用程序。

MVC seemed like a fit at first. MVC起初似乎很合适。 But with UI components nested at various depths (each with their own way of acting on/reacting to model data, and each generating events which they themselves may or may not handle directly), it doesn't seem like MVC can be cleanly applied. 但是,如果UI组件嵌套在不同的深度(每个组件都有自己的方式对模型数据进行操作/对模型数据作出反应,并且每个生成事件本身可能会或可能不会直接处理),看起来MVC似乎不能干净利用。 (But please correct me if that's not the case.) (但如果情况并非如此,请纠正我。)

-- -

( This question resulted in two suggestions of using ajax, which is obviously needed for anything other than the most trivial one-page app.) 这个问题产生了两个使用ajax的建议,除了最简单的单页应用程序之外,其他任何东西显然都需要。)

MVC architecture of PureMVC/JS is the most elegant IMO. PureMVC / JS的 MVC架构是最优雅的IMO。 I learned a lot from it. 我从中学到了很多东西。 I also found Scalable JavaScript Application Architecture by Nicholas Zakas helpful in researching client side architecture options. 我还发现Nicholas Zakas的可扩展JavaScript应用程序架构有助于研究客户端架构选项。

Two other tips 另外两个提示

  1. I've found view, focus, and input management are areas that need special attention in single page web apps 我发现在单页面Web应用程序中需要特别注意视图,焦点和输入管理
  2. I also found it helpful to abstract away the JS library, leaving door open to change mind on what you use, or mix & match should the need arise. 我还发现抽象JS库是有帮助的,让你可以根据需要改变主意,或者在需要时混合搭配。

Nicholas Zakas's presentation as shared by Dean is a very good place to start with. 尼古拉斯·扎卡斯(Nicholas Zakas)的演讲由迪恩分享是一个非常好的开始。 I was also struggling to answer the same question for a while. 我也在努力回答同一个问题一段时间。 After doing couple of large scale Javascript products, thought of sharing the learnings as a reference architecture in case someone needs it. 在做了几个大规模的Javascript产品之后,考虑将学习分享作为参考架构,以防有人需要它。 Have a look at: 看一下:

http://boilerplatejs.org/ http://boilerplatejs.org/

It addresses common Javascript development concerns such as: 它解决了常见的Javascript开发问题,例如:

  • Solution structuring 解决方案结构
  • Creating complex module hierarchy 创建复杂模块层次结构
  • Self contained UI components 自包含的UI组件
  • Event based inter module communication 基于事件的模块间通信
  • Routing, History, Bookmarking 路由,历史,书签
  • Unit Testing 单元测试
  • Localization 本土化
  • Document Generation 文件生成

etc. 等等

The way I build apps: 我构建应用程序的方式:

  • ExtJS framework, single page app, every component defined in a separate JS file, loaded on-demand ExtJS框架,单页面应用程序,在单独的JS文件中定义的每个组件,按需加载
  • Every component contacts its own dedicated web service (sometimes more than one), fetching data into ExtJS stores or special-purpose data structures 每个组件都与其自己的专用Web服务(有时不止一个)联系,将数据提取到ExtJS存储或专用数据结构中
  • The rendering uses standard ExtJS components, so I can bind stores to grids, load forms from records, ... 渲染使用标准的ExtJS组件,因此我可以将商店绑定到网格,从记录中加载表单,...

Just choose a javascript framework, and follow its best practices. 只需选择一个javascript框架,并遵循其最佳实践。 My favorites are ExtJS and GWT, but YMMV. 我最喜欢的是ExtJS和GWT,但是YMMV。

Do NOT roll your own solution for this. 不要为此推出自己的解决方案。 The effort required to duplicate what modern javascript frameworks do is too big. 复制现代javascript框架所需的工作量太大了。 It is always faster to adapt something existing than to build it all from scratch. 适应现有的东西总是比从头开始构建它更快。

Question - What makes an application complex ? 

Answer - The use of word 'complex' in the question itself. 答案 - 在问题本身中使用“复杂”一词。 Hence, a common tendency will be to look out for a complex solution right from the beginning. 因此,一个普遍的趋势是从一开始就寻找复杂的解决方案。

Question - What does the word complex means ?

Answer - Anything that is unknown or partially understood. 答案 - 任何未知或部分理解的东西。 Example : The theory of Gravity even today is COMPLEX to me but not to Sir Isaac Newton who discovered it in 1655. 例子:即使在今天,重力理论对我来说也很复杂,但对于1655年发现它的艾萨克·牛顿爵士却并非如此。

Question - What tools can I use to deal with complexity ?

Answer - Understanding and simplicity. 答案 - 理解和简单。

Question - But I understand my application . Its still complex ?

Answer - Think twice, because understanding and complexity does not co-exist. 答案 - 三思而后行,因为理解和复杂性并不存在。 If you understand a huge huge application, I am sure you will agree that it is nothing but an integration of small and simple units. 如果你了解一个巨大的应用程序,我相信你会同意它只是一个小而简单单元的集成。

Question - Why all of the above philosophical discussion for a question on 
           Single Page Application (SAP)?

Answer - Because, 答案 - 因为,

-> SPA is not some kind of core technology that is newly invented for which we need to reinvent the wheel for a lot of things that we are doing in application development. - > SPA不是新发明的某种核心技术,我们需要为应用程序开发中的许多事情重新发明轮子。

-> Its a concept driven by the need for better performance, availability, scalability and maintainability of web applications. - >它的概念是由对Web应用程序的更好性能,可用性,可伸缩性和可维护性的需求驱动的。

-> Its a fairly newly identified design pattern, so an understanding of SPA as a design pattern goes long way in making informed decisions about the architecture of a SPA. - >它是一个相当新确定的设计模式,因此将SPA作为一种设计模式的理解在做出有关SPA体系结构的明智决策方面有很长的路要走。

-> At the root level no SPA is complex, because after understanding the needs of an application and the SPA pattern, you will realize that you are still creating an application, pretty much the same way you did before with some modifications and re-arrangements in the development approach. - >在根级别没有SPA是复杂的,因为在了解了应用程序和SPA模式的需求之后,您将意识到您仍在创建应用程序,这与您之前进行一些修改和重新安排的方式非常相似在开发方法中。

Question - What about the use of Frameworks ?

Answer - Frameworks are boiler plate code / solution for some commonly identified and generic patterns, hence they can take off x% (variable, based on the application) load from application development but then not a lot should be expected out of them specially for heavy and growing applications. 答案 - 框架是一些通常识别和通用模式的锅炉板代码/解决方案,因此它们可以从应用程序开发中取消x%(基于应用程序的变量)负载,但是不应该特别为它们提供大量的预期和不断增长的应用 Its always a good case to be in complete control of your application structure and flow but most importantly the code for it. 完全控制应用程序结构和流程总是一个很好的例子,但最重要的是它的代码。 There should be no grey or black areas in the application code. 应用程序代码中不应有灰色或黑色区域。

Question - Can you suggest one of the many approaches to SPA architecture ?

Answer - Think of your own framework based on the nature of your application. 答案 - 根据应用程序的性质考虑您自己的框架。 Categorize application components. 对应用程序组件进行分类。 Look for an existing framework that is close to your derived framework, if you find it then use it, if you do not find it then I suggest going ahead with your own. 寻找一个接近你的派生框架的现有框架,如果你发现它然后使用它,如果你没找到它,那么我建议继续你自己的。 Creating framework is quite an effort upfront but produces better results in long run. 创建框架是一项相当大的工作,但从长远来看会产生更好的结果。 Some basic components in my SPA framework will be: 我的SPA框架中的一些基本组件将是:

  • Data Source : Models / Collections of Models 数据来源:模型/模型集合

  • Mark Up for presenting data : Templates 标记为显示数据:模板

  • Interaction with the application : Events 与应用程序的交互:事件

  • State capturing and navigation : Routing 状态捕获和导航:路由

  • Utilities , widgets and plug-ins : libraries 实用程序,小部件和插件:库

Let me know if this helped in any way and good luck with your SPA architecture !! 如果这对您的SPA架构有任何帮助和好运,请告诉我!

The best thing to do is to look at example uses of other frameworks: 最好的办法是查看其他框架的示例用法:

TodoMVC showcases many many SPA frameworks. TodoMVC展示了许多SPA框架。

你可以使用javascript MVC框架http://javascriptmvc.com/

The web application that I am currently working on uses JQuery and I would not recommend it for any large single page web application. 我目前正在使用的Web应用程序使用JQuery,我不建议将其用于任何大型单页Web应用程序。 Most frameworks ie Dojo, yahoo, google and others use namespaces in their libraries but JQuery does not and this is a significant drawback. 大多数框架,如Dojo,yahoo,google和其他框架在其库中使用名称空间,但JQuery没有,这是一个重大缺陷。

If your web site is intended to be small then JQuery would be ok but if you intended to build a large site then I would recommend looking at all the Javascript frameworks available and deciding which one most meets your needs. 如果你的网站很小,那么JQuery就可以,但是如果你打算建一个大型网站,那么我建议你查看所有可用的Javascript框架,并决定哪一个最符合你的需求。

And I would recommend applying the MVC pattern to your javascript/html and probably most of your object model for the javascript could be done as the json that you actually return from the server through ajax and the javascirpt uses the json to render html. 我建议将MVC模式应用到你的javascript / html中,并且你的javascript的大多数对象模型都可以作为你实际从服务器通过ajax返回的json完成,而javascirpt使用json来呈现html。

I would recommend reading the book Ajax in action as it covers most of the stuff you will need to know. 我建议您阅读Ajax的实际应用,因为它涵盖了您需要了解的大部分内容。

我在几页应用程序中使用Samm.js取得了巨大成功

NikaFramework allows you to create single-page application. NikaFramework允许您创建单页面应用程序。 Also allows you to write HTML , CSS ( SASS ), JavaScript into separate files and bundle them into only one output file in the end. 还允许您将HTMLCSSSASS ), JavaScript写入单独的文件,并最终将它们捆绑到一个输出文件中。

I would recommend to explore Yeoman . 我建议去探索Yeoman It allow you to use existing "best practice" for your new project. 它允许您为新项目使用现有的“最佳实践”。

For example: 例如:

if you decide to use Angular.js, there is a Yeoman generator , that give you a structure for routing, views, services, etc. Also allow you to Test, minify your code, etc. 如果您决定使用Angular.js,那么有一个Yeoman生成器 ,它为您提供路由,视图,服务等的结构。还允许您测试,缩小代码等。

If you decide to use Backbone, checkout this generator 如果您决定使用Backbone,请检查此生成器

我会选择jQuery MVC

查看http://bennadel.com/projects/cormvc-jquery-framework.htm Ben非常敏锐,如果你在他的博客上挖掘,他会有一些很好的帖子,关于如何将CorMVC放在一起以及为什么。

Alternative: take a look to ItsNat 替代方案:看看ItsNat

Think in JavaScript but code the same in Java in server with the same DOM APIs, in server is way easier to manage your application without custom client/bridges because UI and data are together. 在JavaScript中思考,但在服务器中使用相同的DOM API在Java中使用相同的代码,在服务器中更容易管理您的应用程序,而无需自定义客户端/网桥,因为UI和数据在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 链接单页Web应用程序的各个部分 - Linking parts of a single-page web application 单页应用程序和网页标题 - Single-page application and web-page title 在单页pushState Web应用程序中模拟画外音页面加载 - Simulate Voiceover page load in single-page pushState web application Javascript和单页应用程序:不允许语音识别 - Javascript and single-page application: speech recognition not-allowed 单页面应用程序的体系结构(JavaScript) - Architecture for single page application (JavaScript) 在单页网页设计上实现链接 - Implement links on single-page web design 具有实际URL的单页应用程序 - Single-Page Application with Real URLs 单页Web应用程序是否应该与服务器保持一个Web Socket连接? - Should a single-page web application keep one Web Socket connection to the server or several? 具有部分的单页javascript应用程序的体系结构 - Architecture of a single page javascript application with sections 使用Amazon Cognito的授权代码授予无服务器/单页Web应用程序 - Using Amazon Cognito's Authorization Code Grant with Serverless/Single-Page Web Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM