move ajax code into library
This commit is contained in:
parent
1d819491f4
commit
bf0326f38c
@ -326,13 +326,21 @@
|
|||||||
$.add(d.head, script);
|
$.add(d.head, script);
|
||||||
return $.rm(script);
|
return $.rm(script);
|
||||||
},
|
},
|
||||||
ajax: function(url, cb, type, event) {
|
ajax: function(url, cb, opts) {
|
||||||
var r;
|
var key, r, val, _ref;
|
||||||
if (type == null) type = 'get';
|
opts.type || (opts.type = 'get');
|
||||||
if (event == null) event = 'onload';
|
opts.event || (opts.event = 'onload');
|
||||||
r = new XMLHttpRequest();
|
r = new XMLHttpRequest();
|
||||||
r[event] = cb;
|
if (opts.headers) {
|
||||||
r.open(type, url, true);
|
_ref = opts.headers;
|
||||||
|
for (key in _ref) {
|
||||||
|
val = _ref[key];
|
||||||
|
r.setRequestHeader(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r[opts.event] = cb;
|
||||||
|
r.open(opts.type, url, true);
|
||||||
|
r.send();
|
||||||
return r;
|
return r;
|
||||||
},
|
},
|
||||||
cache: function(url, cb) {
|
cache: function(url, cb) {
|
||||||
@ -354,7 +362,6 @@
|
|||||||
}
|
}
|
||||||
return _results;
|
return _results;
|
||||||
}));
|
}));
|
||||||
req.send();
|
|
||||||
req.callbacks = [cb];
|
req.callbacks = [cb];
|
||||||
return $.cache.requests[url] = req;
|
return $.cache.requests[url] = req;
|
||||||
}
|
}
|
||||||
@ -2096,9 +2103,11 @@
|
|||||||
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();
|
||||||
updater.request = $.ajax(url, updater.cb.update);
|
return updater.request = $.ajax(url, updater.cb.update, {
|
||||||
updater.request.setRequestHeader('If-Modified-Since', updater.lastModified);
|
headers: {
|
||||||
return updater.request.send();
|
'If-Modified-Since': updater.lastModified
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -2996,12 +3005,14 @@
|
|||||||
thumb = this.previousSibling;
|
thumb = this.previousSibling;
|
||||||
imgExpand.contract(thumb);
|
imgExpand.contract(thumb);
|
||||||
if (engine === 'webkit') {
|
if (engine === 'webkit') {
|
||||||
req = $.ajax(this.src, (function() {
|
return req = $.ajax(this.src, (function() {
|
||||||
if (this.status !== 404) {
|
if (this.status !== 404) {
|
||||||
return setTimeout(imgExpand.retry, 10000, thumb);
|
return setTimeout(imgExpand.retry, 10000, thumb);
|
||||||
}
|
}
|
||||||
}), 'head', 'onreadystatechange');
|
}), {
|
||||||
return req.send();
|
type: 'head',
|
||||||
|
event: 'onreadystatechange'
|
||||||
|
});
|
||||||
} else if (!g.dead) {
|
} else if (!g.dead) {
|
||||||
return setTimeout(imgExpand.retry, 10000, thumb);
|
return setTimeout(imgExpand.retry, 10000, thumb);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -231,10 +231,16 @@ $.extend $,
|
|||||||
textContent: "(#{code})()"
|
textContent: "(#{code})()"
|
||||||
$.add d.head, script
|
$.add d.head, script
|
||||||
$.rm script
|
$.rm script
|
||||||
ajax: (url, cb, type='get', event='onload') ->
|
ajax: (url, cb, opts) ->
|
||||||
|
opts.type or= 'get'
|
||||||
|
opts.event or= 'onload'
|
||||||
r = new XMLHttpRequest()
|
r = new XMLHttpRequest()
|
||||||
r[event] = cb
|
if opts.headers
|
||||||
r.open type, url, true
|
for key, val of opts.headers
|
||||||
|
r.setRequestHeader key, val
|
||||||
|
r[opts.event] = cb
|
||||||
|
r.open opts.type, url, true
|
||||||
|
r.send()
|
||||||
r
|
r
|
||||||
cache: (url, cb) ->
|
cache: (url, cb) ->
|
||||||
if req = $.cache.requests[url]
|
if req = $.cache.requests[url]
|
||||||
@ -244,7 +250,6 @@ $.extend $,
|
|||||||
req.callbacks.push cb
|
req.callbacks.push cb
|
||||||
else
|
else
|
||||||
req = $.ajax url, (-> cb.call @ for cb in @callbacks)
|
req = $.ajax url, (-> cb.call @ for cb in @callbacks)
|
||||||
req.send()
|
|
||||||
req.callbacks = [cb]
|
req.callbacks = [cb]
|
||||||
$.cache.requests[url] = req
|
$.cache.requests[url] = req
|
||||||
cb:
|
cb:
|
||||||
@ -1684,9 +1689,7 @@ 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
|
updater.request = $.ajax url, updater.cb.update, headers: 'If-Modified-Since': updater.lastModified
|
||||||
updater.request.setRequestHeader 'If-Modified-Since', updater.lastModified
|
|
||||||
updater.request.send()
|
|
||||||
|
|
||||||
watcher =
|
watcher =
|
||||||
init: ->
|
init: ->
|
||||||
@ -2321,8 +2324,7 @@ imgExpand =
|
|||||||
if engine is 'webkit'
|
if engine is 'webkit'
|
||||||
req = $.ajax @src, (->
|
req = $.ajax @src, (->
|
||||||
setTimeout imgExpand.retry, 10000, thumb if @status isnt 404
|
setTimeout imgExpand.retry, 10000, thumb if @status isnt 404
|
||||||
), 'head', 'onreadystatechange'
|
), type: 'head', event: 'onreadystatechange'
|
||||||
req.send()
|
|
||||||
#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