jQuery 自定义 ajax 构建

发布于 12-02 20:39 字数 1655 浏览 0 评论 0原文

我试图从 jQuery 中删除除 ajax 功能之外的所有内容。很抱歉问了这个愚蠢的问题,但请听我说完:

  • 我正在使用 Web Workers 进行 ajax 调用,所以我不能在其中包含 core.js 的东西。
  • 我使用的另一个库称为 jQuery,但我无法让它与 jQuery.Hive.Pollen 库一起使用。经过几个小时的故障排除后,这个 ChemDoodle Web 组件库似乎只能与 jQuery 一起使用,我的经验不足,不知道为什么。
  • 使用 makefile 构建 jQuery 似乎很容易,但我不确定要包含什么。在makefile中,我把除了ajax相关的东西之外的所有东西都拿出来了:

.

BASE_FILES = ${SRC_DIR}/ajax.js\
    ${SRC_DIR}/ajax/jsonp.js\
    ${SRC_DIR}/ajax/script.js\
    ${SRC_DIR}/ajax/xhr.js\

然而,对 /ajax 源文件的进一步检查显示了调用“文档”的方法。那么这意味着无论我剥离多少个模块,这都根本不起作用?

更新:jQuery 上的错误问题 #9889 似乎表明 core 永远不会提供可供 Web 工作人员使用的 ajax 函数,至少不会很快。

为任何感兴趣的人描述一下我正在尝试做的事情以及为什么我需要使用网络工作者。我正在放置一个分布式计算应用程序,它可以从化合物数据库中进行一些分子模拟/筛选。普通的 javascript 不起作用,因为它会减慢页面速度,并且我希望应用程序在浏览器中持续运行,计算一个又一个分子。这是我想要的网络工作者的伪代码大纲

while (true){main();}

main(){
 get job from server via ajax
 process data//must use web worker
send results to server via ajax
}

是的,我可以实现我的程序来反序列化从主页传递的数据字符串,并让页面获取ajax数据,就像这样

//page-side javascript
var slave = new Worker()
slave.onMessage(event){
if status = "ready" {
check if data is available- if so, send it to server via ajax
fetch job data from server via ajax
slave.postMessage(job data)
}
}

//worker (slave)
self.onmessage(data) {
//process results
//send back to page
}

我想这可以工作,但它有点俗气,可能比让工人做所有事情要慢。根据 Rick Waldron 的说法,“如果您有一个轮询的系统,例如,打开一个工作程序,在每次响应时永远循环发出 xhr 请求,将结果 postMessage() 返回给客户端,然后你可以受益”

我想我会永远在循环中发出 xhr 请求,所以这个应用程序将是有益的。

我确实找到了 jQuery.Hive.pollen.js,但它无法与我的其他库一起使用来进行我需要的 ajax 调用。不过,这是一个单独的问题(实际上是我的主要问题),所以如果有人感兴趣,这里有一个链接

I'm trying to strip everything except for ajax functionality from jQuery. Sorry for the silly question, but hear me out:

  • I'm using web workers to make the ajax call so I can't have the core.js stuff in there.
  • Another library I'm using calls jQuery and I haven't been able to get it to work with the jQuery.Hive.Pollen library. After many hours of troubleshooting it seems like this ChemDoodle Web Components library only works with jQuery and I'm not experienced enough to know why.
  • Seems like a pretty easy thing to do to use the makefile to build jQuery, but I'm not sure what to include. In the makefile, I took out everything except the ajax-related stuff:

.

BASE_FILES = ${SRC_DIR}/ajax.js\
    ${SRC_DIR}/ajax/jsonp.js\
    ${SRC_DIR}/ajax/script.js\
    ${SRC_DIR}/ajax/xhr.js\

However, further inspection of the /ajax source files shows methods that call 'document'. So that means that this won't work at all no matter how many modules I strip right?

Update: Bug issue #9889 on jQuery seems to indicate that core will never provide an ajax function usable by web workers, at least not anytime soon.

A bit of a description of what I am trying to do and why I need to use web workers, for anybody interested. I'm putting a distributed computing application that does some molecular simulation/screening from a database of compounds. Normal javascript won't work because it will slow down the page and I want the application to continuously run in the browser, computing one molecule after another. Here's a pseudocode outline of my desired web worker

while (true){main();}

main(){
 get job from server via ajax
 process data//must use web worker
send results to server via ajax
}

Yes, I could implement my program to unserialize a data string passed from the main page and get the page to fetch the ajax data, like so

//page-side javascript
var slave = new Worker()
slave.onMessage(event){
if status = "ready" {
check if data is available- if so, send it to server via ajax
fetch job data from server via ajax
slave.postMessage(job data)
}
}

//worker (slave)
self.onmessage(data) {
//process results
//send back to page
}

I guess this could work but it's a little tacky and probably is slower than just having the worker do everything. According to Rick Waldron, "if you had a system that was polling eg. open a worker, make xhr requests in a loop forever, on each response, postMessage() the results back to the client, then you can benefit"

I guess I am making xhr requests in a loop forever so this application would be beneficial.

I did find jQuery.Hive.pollen.js, but it is not working with my other library that makes the ajax call I need. It's a separate question though (actually my main question), so if anybody is interested here is a link to that post:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像你2024-12-09 20:39:56

啊,我还没有弄清楚 jQuery 问题,但我现在知道如何使 pollen.js 更像 jQuery。

删除(或注释掉)pollen中的两个请求头:

_xhr.setRequestHeader("X-Requested-With", "Worker-XMLHttpRequest");
_xhr.setRequestHeader("X-Worker-Hive", "Pollen-JS" );

与jQuery不同,pollen.js的ajax函数返回一个JSON对象,其中包含服务器返回的JSON数据以及字符串化形式。

data = {"json":{jsonobjecthere},"text":"stringifiedversionofthedata"}

如果进行这两个修改,那么我相信 pollen 几乎与 jQuery 的 ajax 相同

Ah, I haven't figured the jQuery issue out but I know now how to make pollen.js more like jQuery.

Delete (or comment out) the two request headers in pollen:

_xhr.setRequestHeader("X-Requested-With", "Worker-XMLHttpRequest");
_xhr.setRequestHeader("X-Worker-Hive", "Pollen-JS" );

and unlike jQuery, pollen.js's ajax function returns a JSON object containing the JSON data returned by the server as well as a stringified form.

data = {"json":{jsonobjecthere},"text":"stringifiedversionofthedata"}

If those two modifications are made, then I believe pollen is nearly the same as jQuery's ajax

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文