开发者

Is it possible to chain jquery ajax requests as a one level queue (without nesting)

The question initially arose from a idea to dynamically load a group of javascript files with $.getScript.

I am developing a js-only front-ends of a couple of projects, which share some libraries, and it would be nice (or at least it seems) to have a one js file which handles common libraries includes, and also the other one - which handles specific libraries.

Some of the front-ends I am talking about will actually work as widgets to the other websites, and until we setup a process of js file compression into one, that is also needed to place a widget as a single js include, and then include all the dependencies in a queue.

Something like: $.getScript .getScript .getScript

So the question has two parts: 1. Would it make sense? 开发者_如何学JAVA2. If yes, how?

Thanks!


do you mean the following: you want to write something like

$.getScript("js/file1.js").getScript("js/file2.js").getScript("js/file3.js");

and as result load one file from server, which contains all specified (sort of file1.js + file2.js + file3.js), don't you? If I get it, then It seems to be impossible, at least that way. It really makes some sense, even good one, because browser will send one request instead of three (so performance will be better). To implement what you want, you need good support from server side. E.g. you could write

$.getScript("js/libraries?files=file1,file2,file3");

and server side should be able to parse this request, append all files mentioned in request, and send you. But as you see, something should exist on server side to assist you;

If you develop some framework or such, you can make a predefined build, e.g. your library will contain

js/
    file1.js
    file2.js
    file3.js
common.js

where common.js is just file1.js + file2.js + file3.js, and you can automate it, so you will change your code, and then run a special build which will create common.js. This way you can rely on

$.getScript("common.js");

and that's even better than requesting

$.getScript("js/libraries?files=file1,file2,file3");

because in case of last one some runtime job has to be performed (performance suffers a bit). when we develop our projects we use jawr on server side to make predefined bundles of javascript libraries/files

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜