简体   繁体   English

单页面应用程序的体系结构(JavaScript)

[英]Architecture for single page application (JavaScript)

I wanted to understand the overall architecture for designing single page application (mainly through JavaScript) 我想了解设计单页面应用程序的整体架构(主要通过JavaScript)

Let's say I have a login page (Usernam/Password) and on successful authentication, I am shown the homepage. 假设我有一个登录页面(Usernam / Password),并且在成功验证后,我会看到主页。 Now both the login and homepage screens should actually be part of a single page. 现在登录和主页屏幕实际上应该是单个页面的一部分。

How can I design such kind of page? 我该如何设计这样的页面?

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. 基本上你需要做的是使用AJAX而不是页面重新加载。 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.). 您仍然需要连接到服务器以对用户进行身份验证,但不是每次都重新加载整个页面,而是需要对服务器进行AJAX调用,具体取决于登录是否成功以及是否更改内容的某些部分在页面上(例如将登录按钮更改为“以用户xxx登录”消息等)。

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 如果您还没有看过,John Papa有一个非常受欢迎的关于在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. 它确实需要Pluralsight订阅,但您可以获得10天的免费试用,以确认内容对您有价值。 (I am not affiliated with Pluralsight, btw.) (我不是Pluralsight的附属品,顺便说一下。)

You may want to look up this free Single Page App book . 您可能想要查找这个免费的单页应用程序书 I found it when I Googled "Single Page Apps". 当我用Google搜索“单页应用”时,我发现了它。

You may take inspiration from existing solutions that you can find over the web : 您可以从通过网络找到的现有解决方案中获取灵感:

I Just added a project to SourceForge that may help. 我刚刚向SourceForge添加了一个可能有帮助的项目。 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. 该项目允许您从JavaScript中引用asp.net MVC。

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

This library generates dynamic JavaScript code to use for AJAX operations. 该库生成用于AJAX操作的动态JavaScript代码。 The library requires jQuery. 该库需要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: 例如,如果在Controller中使用Action方法进行AJAX操作,则可以通过动态生成的JavaScript文件快速访问它,如下所示:

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

The mapped parameters are directly associated with the ActionMethods parameters. 映射的参数与ActionMethods参数直接关联。

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 自定义后请求处理器

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. 如果你是一个MVC的人,我个人一直在使用基于主干的chaplinjs.org和单页应用程序荣耀的hbs。 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) 我有一些修改使用websockets而不是长轮询,但它非常可扩展,如果你熟悉mvc,很容易进入(你会遇到更多问题,骨干比Chaplin)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM