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

一个复杂的单页 JS Web 应用程序应该如何在客户端进行结构化?具体来说,我很好奇如何根据模型对象、 UI 组件、任何控制器和处理服务器持久性的对象来清晰地构造应用程序。

一开始车祸看起来很严重。但是,由于 UI 组件嵌套在不同的深度(每个组件对模型数据都有自己的作用/反应方式,并且每个组件生成的事件可能自己直接处理,也可能不直接处理) ,MVC 似乎无法被清晰地应用。(但如果事实并非如此,请纠正我。)

--

(这个问题提出了使用 ajax 的两个建议,很明显,除了最普通的单页面应用程序之外,任何其他应用程序都需要 Ajax。)

51183 次浏览

I would go with jQuery MVC

Check out http://bennadel.com/projects/cormvc-jquery-framework.htm Ben is pretty sharp and if you dig around on his blog he has some nice posts about how CorMVC is put together and why.

You can use javascript MVC framework http://javascriptmvc.com/

The way I build apps:

  • ExtJS framework, single page app, every component defined in a separate JS file, loaded on-demand
  • Every component contacts its own dedicated web service (sometimes more than one), fetching data into ExtJS stores or special-purpose data structures
  • The rendering uses standard ExtJS components, so I can bind stores to grids, load forms from records, ...

Just choose a javascript framework, and follow its best practices. My favorites are ExtJS and GWT, but YMMV.

Do NOT roll your own solution for this. The effort required to duplicate what modern javascript frameworks do is too big. It is always faster to adapt something existing than to build it all from scratch.

MVC architecture of PureMVC/JS is the most elegant IMO. I learned a lot from it. I also found Scalable JavaScript Application Architecture by Nicholas Zakas helpful in researching client side architecture options.

Two other tips

  1. I've found view, focus, and input management are areas that need special attention in single page web apps
  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.

The web application that I am currently working on uses JQuery and I would not recommend it for any large single page web application. Most frameworks i.e. Dojo, yahoo, google and others use namespaces in their libraries but JQuery does not and this is a significant drawback.

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.

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.

I would recommend reading the book Ajax in action as it covers most of the stuff you will need to know.

Alternative: take a look to 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.

I'm using Samm.js in several one page applications with great success

Nicholas Zakas's presentation as shared by Dean is a very good place to start with. 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. Have a look at:

http://boilerplatejs.org/

It addresses common Javascript development concerns such as:

  • Solution structuring
  • Creating complex module hierarchy
  • Self contained UI components
  • Event based inter module communication
  • Routing, History, Bookmarking
  • Unit Testing
  • Localization
  • Document Generation

etc.

The best thing to do is to look at example uses of other frameworks:

TodoMVC showcases many many SPA frameworks.

NikaFramework allows you to create single-page application. Also allows you to write HTML, CSS (SASS), JavaScript into separate files and bundle them into only one output file in the end.

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.

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.

-> Its a concept driven by the need for better performance, availability, scalability and maintainability of web applications.

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

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

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

  • 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 !!

I would recommend to explore 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.

If you decide to use Backbone, checkout this generator