如何创建 AJAX 半同步行为
上个月的大部分时间我都在苦苦挣扎,直到我想出了一种简单的方法来动态加载并将存储在服务器上的 HTML 画布类链接在一起,但显然,在客户端上初始化(比当排序在异步环境中很重要时,它会发出声音)。
我想知道是否有人可以帮助我找到一种加载简单 JavaScript 脚本的方法。让我们定义一个 load('foo.js')
函数,它指示客户端从服务器加载脚本 foo.js
并将其作为 JavaScript 代码执行。
给定存储在服务器上的三个文件:
A.js
a = 10;
B.js
load('A.js');
b = a + 10;
C.js
load('B.js');
c = b + 10;
如果客户端发出命令 load('C.js');
最简单/最可靠的实现方法是什么这。我的一个想法是扫描服务器端的代码并立即返回所有脚本。这需要最少数量的 php 请求。但是,如果客户端之前已经请求过 C.js
,则该脚本应该存在于客户端,而这将是低效的,特别是如果 C.js
及其所有依赖文件很大。我考虑的另一个选择是将所有这些服务器端脚本包装在一个对象中,如下所示,对于上面的 C.js:
{
depenencies: ['B.js'] ,
code : 'c.age = b.age + 10;'
}
我只是不知道如何“暂停”脚本 C 的执行。在 load('B.js')
语句之后调用 js,然后在加载 B.js
后恢复它。
编辑 感谢 redsqaure 建议 yepnope 和 requirejs。不幸的是,我不喜欢它们有几个原因。其一,requirejs 很困难(我确信我会因此而受到批评)。我对此的主要抱怨是,如果它这么难学,我还不如自己重新创建它,在这个过程中学习它,并更好地控制它。其次,它需要你改变你的写作风格。切换到 Dojo 并必须使用 dojo.declare("ClassName", [ParentA,ParentB], {...}); 来声明类是一回事,但包装 everyrequire(['A','B',...], function(){}); 中的strong> 代码片段是另一个。最后,我不知道指示在哪里查找文件会有多简单。我希望用户能够定义“PATH”变量服务器端,并在“PATH”的每个文件夹/子文件夹中进行搜索
I spent the better part of last month beating my head against the wall before I came up with an easy way to dynamically load, and chain together HTML canvas classes which are stored on the server, but, obviously, initialized on the client (harder than it sounds when the ordering is important in an asynchronous environment).
I was wondering if someone could help me find a way to load simple javascript scripts. Lets define a load('foo.js')
function which instructs the client to load script foo.js
from the server and execute it as javascript code.
Given the three files, stored on the server:
A.js
a = 10;
B.js
load('A.js');
b = a + 10;
C.js
load('B.js');
c = b + 10;
If the client issues the command load('C.js');
what's the easiest/most reliable way to implement this. One idea I had was to scan the code serverside and return all the scripts at once. This requires the minimal amount of php requests. However, if the client has already requested C.js
before, the script should exist client side, and this would be inneficient, especially if C.js
and all its dependent files are large. Another option I considered was to wrap all of these serverside scripts in an object like so, for C.js
above:
{
depenencies: ['B.js'] ,
code : 'c.age = b.age + 10;'
}
I just don't know how to 'pause' execution of script C.js
after the load('B.js')
statement, and then resuming it after B.js
has been loaded.
EDIT Thanks to redsqaure for suggesting yepnope and requirejs. Unfortunately, I do not like them for several reasons. For one, requirejs is difficult (I am sure I will come under criticism for this one). My main gripe with this is that, if it is so difficult to learn, I might as well recreate it myself, learning it in the process, AND having greater control over it. Second, it requires you to change your style of writing. Switching to Dojo and having to use dojo.declare("ClassName", [ParentA,ParentB], {...});
to declare classes is one thing, but wrapping every snippet of code in require(['A','B',...], function(){});
is another. Finally, I don't know how simple it will be to instruct where to look for files. I want the user to be able to define a 'PATH' variable server side, and have the search occur in each of the folders/subfolders of the 'PATH'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取决于您想要的优化程度。您可以采用同步 XHR 的方式或使用回调(异步且推荐)。如果您选择第二条路线,您的代码将类似于:
编辑
在反馈您想要的内容后再看一下是可能的,但会很脆弱并且很难用 JavaScript 编写。下面我有一个依赖加载器的示例/未经测试的实现,其中一个文件只能有一次对 load("file.js") 的调用。对于多个可能的依赖关系,这会更加复杂。我还假设这些文件来自同一域。
Depends on how optimized you want it to be. Either you can go the route of synchronous XHR or use a callback (async and recommended). If you were to go the second route your code would look something like:
EDIT
Taking a second look after you feedback what you want is somewhat possible, but would be brittle and hard to write in javascript. Below i have a sample/untested implementation of a dependency loader where a file can only have one call to load("file.js") possible. This would be more complex for multiple possible dependencies. Also I'm assuming these files are coming from the same domain.
为什么不研究像 yepnope.js 或 require.js
Why not look into a script loader like yepnope.js or require.js