Architecture of a single-page JavaScript web appli

2019-01-16 00:00发布

How should a complex single-page JS web application be structured on the client-side? 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.

MVC seemed like a fit at first. 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. (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.)

14条回答
\"骚年 ilove
2楼-- · 2019-01-16 00:21

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

TodoMVC showcases many many SPA frameworks.

查看更多
The star\"
3楼-- · 2019-01-16 00:26

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.

查看更多
虎瘦雄心在
4楼-- · 2019-01-16 00:28

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

查看更多
贼婆χ
5楼-- · 2019-01-16 00:29
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 !!

查看更多
疯言疯语
6楼-- · 2019-01-16 00:29

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

查看更多
闹够了就滚
7楼-- · 2019-01-16 00:33

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.

查看更多
登录 后发表回答