jQuery 自定义 ajax 构建
我试图从 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:
啊,我还没有弄清楚 jQuery 问题,但我现在知道如何使 pollen.js 更像 jQuery。
删除(或注释掉)pollen中的两个请求头:
与jQuery不同,pollen.js的ajax函数返回一个JSON对象,其中包含服务器返回的JSON数据以及字符串化形式。
如果进行这两个修改,那么我相信 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:
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.
If those two modifications are made, then I believe pollen is nearly the same as jQuery's ajax