Architecture for single page application (JavaScript)

前端 未结 7 1975
旧时难觅i
旧时难觅i 2020-12-15 10:27

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) a

相关标签:
7条回答
  • 2020-12-15 10:59

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

    0 讨论(0)
  • 2020-12-15 11:02

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

    0 讨论(0)
  • 2020-12-15 11:02

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

    0 讨论(0)
  • 2020-12-15 11:03

    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)

    0 讨论(0)
  • 2020-12-15 11:10

    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.

    0 讨论(0)
  • 2020-12-15 11:14

    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
    0 讨论(0)
提交回复
热议问题