Architecture for single page application (JavaScri

2020-02-09 04:40发布

问题:

I wanted to understand the overall architecture for designing single page application (mainly through JavaScript)

Let's say I have a login page (Usernam/Password) and on successful authentication, I am shown the homepage. Now both the login and homepage screens should actually be part of a single page.

How can I design such kind of page?

回答1:

This is such a broad question that entire books could be written to answer it in detail.

Basically what you need to do is to use AJAX instead of page reloads. You still need to connect to the server to authenticate users but instead of reloading the entire page every time you do it, you need to make an AJAX call to your server and depending on whether the login was successful or not change some parts of the content on the page (like changing the Login button to a "Logged in as user xxx" message etc.).



回答2:

If you haven't seen it already, John Papa has a very popular course on designing Single Page Applications on Pluralsight: http://www.pluralsight.com/training/Courses/TableOfContents/single-page-apps-jumpstart

It does require a Pluralsight subscription, but you can get a 10 day free trial to confirm the content is valuable to you. (I am not affiliated with Pluralsight, btw.)



回答3:

You may want to look up this free Single Page App book. I found it when I Googled "Single Page Apps".



回答4:

You may take inspiration from existing solutions that you can find over the web :

  • ExtJS (MVC application architecture)
  • Knockout (built upon the MVVM pattern)
  • Backbone


回答5:

I Just added a project to SourceForge that may help. I've been developing this library for about a year now, and I think it's ready for prime-time. This project allows for you to reference asp.net MVC from within JavaScript.

https://sourceforge.net/projects/peachajax/

This library generates dynamic JavaScript code to use for AJAX operations. The library requires jQuery. For example, if you use an Action Method within a Controller for AJAX operation, you can quickly access this via the dynamically generated JavaScript file as follows:

peach.ControllerName.ActionMethodName(parameter1, parameter2, parameter3); // javascript

The mapped parameters are directly associated with the ActionMethods parameters.

Customization features include:

  • Custom parameters
  • Custom callbacks
  • Custom client-side processing functions for parameters (for serializing specialized model types)
  • Custom pre-request processors
  • Custom post-request processors


回答6:

If you're more an MVC guy, I personally have been using chaplinjs.org, which is based on backbone, and hbs for single-page app glory. I have a few modifications to use websockets instead of long-polls, but it's pretty quite extendible, and if you're familiar with mvc, easy enough to get into (you'll run into more problems with backbone than with Chaplin)



回答7:

You need index.html page like below

<html>
  <body>
    <div id="header"><!-- something cool here --></div> 
      <div id="login" class="view"> ... </div> 
      <div id="home" class="view" style="display:none;"> ... </div>
      <div id="page3" class="view" style="display:none;"> ... </div>
      <div id="page4" class="view" style="display:none;"> ... </div>

     <div id="footer"><!-- something cool here --></div>
  </body>
</html>  

When application loaded, all views (divs with view class) are hidden (display:none) except login view. Login view should have login form on it, when it submitted by user it initiates ajax request. When ajax successful application hides login page and displays home page instead.

You may structure your code next way. For every module you will have model, view and controller files.

For example, for login module you may have loginModel.js, loginView.js, loginCtrl.js. In model file you will connect to DB and check credentials provided. In view you will bind listeners to controls. In controller you will react to user pressed Submit button.