Refactor $.ajax.
This commit is contained in:
parent
6bc0fb18d9
commit
58da6d4e18
@ -320,20 +320,18 @@
|
|||||||
id: function(id) {
|
id: function(id) {
|
||||||
return d.getElementById(id);
|
return d.getElementById(id);
|
||||||
},
|
},
|
||||||
ajax: function(url, cb, opts) {
|
ajax: function(url, callbacks, opts) {
|
||||||
var event, form, headers, key, onprogress, r, type, val;
|
var form, headers, key, r, type, upCallbacks, val;
|
||||||
if (opts == null) opts = {};
|
if (opts == null) opts = {};
|
||||||
type = opts.type, event = opts.event, headers = opts.headers, form = opts.form, onprogress = opts.onprogress;
|
type = opts.type, headers = opts.headers, upCallbacks = opts.upCallbacks, form = opts.form;
|
||||||
type || (type = 'get');
|
|
||||||
event || (event = 'onload');
|
|
||||||
r = new XMLHttpRequest();
|
r = new XMLHttpRequest();
|
||||||
r.open(type, url, true);
|
r.open(type || 'get', url, true);
|
||||||
for (key in headers) {
|
for (key in headers) {
|
||||||
val = headers[key];
|
val = headers[key];
|
||||||
r.setRequestHeader(key, val);
|
r.setRequestHeader(key, val);
|
||||||
}
|
}
|
||||||
r[event] = cb;
|
$.extend(r, callbacks);
|
||||||
r.upload.onprogress = onprogress;
|
$.extend(r.upload, upCallbacks);
|
||||||
r.send(form);
|
r.send(form);
|
||||||
return r;
|
return r;
|
||||||
},
|
},
|
||||||
@ -346,7 +344,8 @@
|
|||||||
return req.callbacks.push(cb);
|
return req.callbacks.push(cb);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
req = $.ajax(url, (function() {
|
req = $.ajax(url, {
|
||||||
|
onload: (function() {
|
||||||
var cb, _i, _len, _ref, _results;
|
var cb, _i, _len, _ref, _results;
|
||||||
_ref = this.callbacks;
|
_ref = this.callbacks;
|
||||||
_results = [];
|
_results = [];
|
||||||
@ -355,7 +354,8 @@
|
|||||||
_results.push(cb.call(this));
|
_results.push(cb.call(this));
|
||||||
}
|
}
|
||||||
return _results;
|
return _results;
|
||||||
}));
|
})
|
||||||
|
});
|
||||||
req.callbacks = [cb];
|
req.callbacks = [cb];
|
||||||
return $.cache.requests[url] = req;
|
return $.cache.requests[url] = req;
|
||||||
}
|
}
|
||||||
@ -1716,20 +1716,24 @@
|
|||||||
val = data[name];
|
val = data[name];
|
||||||
if (val) form.append(name, val);
|
if (val) form.append(name, val);
|
||||||
}
|
}
|
||||||
return qr.ajax = $.ajax(url, (function() {
|
return qr.ajax = $.ajax(url, {
|
||||||
|
onload: (function() {
|
||||||
return qr.message.send({
|
return qr.message.send({
|
||||||
response: true,
|
response: true,
|
||||||
html: this.response
|
html: this.response
|
||||||
});
|
});
|
||||||
}), {
|
})
|
||||||
type: 'post',
|
}, {
|
||||||
form: form,
|
form: form,
|
||||||
|
type: 'post',
|
||||||
|
upCallbacks: {
|
||||||
onprogress: function(e) {
|
onprogress: function(e) {
|
||||||
return qr.message.send({
|
return qr.message.send({
|
||||||
status: true,
|
status: true,
|
||||||
progress: Math.floor(e.loaded / e.total * 100)
|
progress: Math.floor(e.loaded / e.total * 100)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2252,7 +2256,9 @@
|
|||||||
updater.timer.textContent = 0;
|
updater.timer.textContent = 0;
|
||||||
if ((_ref = updater.request) != null) _ref.abort();
|
if ((_ref = updater.request) != null) _ref.abort();
|
||||||
url = location.pathname + '?' + Date.now();
|
url = location.pathname + '?' + Date.now();
|
||||||
return updater.request = $.ajax(url, updater.cb.update, {
|
return updater.request = $.ajax(url, {
|
||||||
|
onload: updater.cb.update
|
||||||
|
}, {
|
||||||
headers: {
|
headers: {
|
||||||
'If-Modified-Since': updater.lastModified
|
'If-Modified-Since': updater.lastModified
|
||||||
}
|
}
|
||||||
@ -3181,13 +3187,14 @@
|
|||||||
}
|
}
|
||||||
url = href + '?' + Date.now();
|
url = href + '?' + Date.now();
|
||||||
if (engine === 'webkit') {
|
if (engine === 'webkit') {
|
||||||
return req = $.ajax(this.src, (function() {
|
return req = $.ajax(this.src, {
|
||||||
|
onreadystatechange: (function() {
|
||||||
if (this.status !== 404) {
|
if (this.status !== 404) {
|
||||||
return setTimeout(imgExpand.expand, 10000, thumb, url);
|
return setTimeout(imgExpand.expand, 10000, thumb, url);
|
||||||
}
|
}
|
||||||
}), {
|
})
|
||||||
type: 'head',
|
}, {
|
||||||
event: 'onreadystatechange'
|
type: 'head'
|
||||||
});
|
});
|
||||||
} else if (!g.dead) {
|
} else if (!g.dead) {
|
||||||
return setTimeout(imgExpand.expand, 10000, thumb, url);
|
return setTimeout(imgExpand.expand, 10000, thumb, url);
|
||||||
|
|||||||
@ -229,16 +229,14 @@ $.extend $,
|
|||||||
$.on d, 'DOMContentLoaded', cb
|
$.on d, 'DOMContentLoaded', cb
|
||||||
id: (id) ->
|
id: (id) ->
|
||||||
d.getElementById id
|
d.getElementById id
|
||||||
ajax: (url, cb, opts={}) ->
|
ajax: (url, callbacks, opts={}) ->
|
||||||
{type, event, headers, form, onprogress} = opts
|
{type, headers, upCallbacks, form} = opts
|
||||||
type or= 'get'
|
|
||||||
event or= 'onload'
|
|
||||||
r = new XMLHttpRequest()
|
r = new XMLHttpRequest()
|
||||||
r.open type, url, true
|
r.open type or 'get', url, true
|
||||||
for key, val of headers
|
for key, val of headers
|
||||||
r.setRequestHeader key, val
|
r.setRequestHeader key, val
|
||||||
r[event] = cb
|
$.extend r, callbacks
|
||||||
r.upload.onprogress = onprogress
|
$.extend r.upload, upCallbacks
|
||||||
r.send form
|
r.send form
|
||||||
r
|
r
|
||||||
cache: (url, cb) ->
|
cache: (url, cb) ->
|
||||||
@ -248,7 +246,7 @@ $.extend $,
|
|||||||
else
|
else
|
||||||
req.callbacks.push cb
|
req.callbacks.push cb
|
||||||
else
|
else
|
||||||
req = $.ajax url, (-> cb.call @ for cb in @callbacks)
|
req = $.ajax url, onload: (-> cb.call @ for cb in @callbacks)
|
||||||
req.callbacks = [cb]
|
req.callbacks = [cb]
|
||||||
$.cache.requests[url] = req
|
$.cache.requests[url] = req
|
||||||
cb:
|
cb:
|
||||||
@ -1308,10 +1306,14 @@ qr =
|
|||||||
delete data.upfile
|
delete data.upfile
|
||||||
for name, val of data
|
for name, val of data
|
||||||
form.append name, val if val
|
form.append name, val if val
|
||||||
qr.ajax = $.ajax url, (-> qr.message.send response: true, html: @response),
|
qr.ajax = $.ajax url, onload: (-> qr.message.send response: true, html: @response),
|
||||||
type: 'post',
|
form: form
|
||||||
form: form,
|
type: 'post'
|
||||||
onprogress: (e) -> qr.message.send status: true, progress: Math.floor e.loaded / e.total * 100
|
upCallbacks:
|
||||||
|
onprogress: (e) ->
|
||||||
|
qr.message.send
|
||||||
|
status: true
|
||||||
|
progress: Math.floor e.loaded / e.total * 100
|
||||||
|
|
||||||
options =
|
options =
|
||||||
init: ->
|
init: ->
|
||||||
@ -1771,7 +1773,8 @@ updater =
|
|||||||
updater.request?.abort()
|
updater.request?.abort()
|
||||||
#fool the cache
|
#fool the cache
|
||||||
url = location.pathname + '?' + Date.now()
|
url = location.pathname + '?' + Date.now()
|
||||||
updater.request = $.ajax url, updater.cb.update, headers: 'If-Modified-Since': updater.lastModified
|
updater.request = $.ajax url, onload: updater.cb.update,
|
||||||
|
headers: 'If-Modified-Since': updater.lastModified
|
||||||
|
|
||||||
watcher =
|
watcher =
|
||||||
init: ->
|
init: ->
|
||||||
@ -2422,9 +2425,8 @@ imgExpand =
|
|||||||
url = href + '?' + Date.now()
|
url = href + '?' + Date.now()
|
||||||
#navigator.online is not x-browser/os yet
|
#navigator.online is not x-browser/os yet
|
||||||
if engine is 'webkit'
|
if engine is 'webkit'
|
||||||
req = $.ajax @src, (->
|
req = $.ajax @src, onreadystatechange: (-> setTimeout imgExpand.expand, 10000, thumb, url if @status isnt 404),
|
||||||
setTimeout imgExpand.expand, 10000, thumb, url if @status isnt 404
|
type: 'head'
|
||||||
), type: 'head', event: 'onreadystatechange'
|
|
||||||
#Firefox returns a status code of 0 because of the same origin policy
|
#Firefox returns a status code of 0 because of the same origin policy
|
||||||
#Oprah doesn't send any request
|
#Oprah doesn't send any request
|
||||||
else unless g.dead
|
else unless g.dead
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user