Slightly simplify $.ajax.

This commit is contained in:
Mayhem 2013-08-07 19:55:25 +02:00
parent bbe93eb475
commit 39f30c1d0e
4 changed files with 9 additions and 17 deletions

View File

@ -36,23 +36,15 @@ $.extend = (object, properties) ->
for key, val of properties
object[key] = val
return
$.ajax = (url, callbacks, opts={}) ->
{type, cred, headers, upCallbacks, form, sync} = opts
$.ajax = (url, options, extra={}) ->
{type, headers, upCallbacks, form, sync} = extra
r = new XMLHttpRequest()
type or= form and 'post' or 'get'
r.open type, url, !sync
for key, val of headers
r.setRequestHeader key, val
$.extend r, callbacks
$.extend r, options
$.extend r.upload, upCallbacks
try
# Firefox throws an error if you try
# to set this on a synchronous XHR.
# Only cookies from the remote domain
# are used in a request withCredentials.
r.withCredentials = cred
catch err
# do nothing
r.send form
r
$.cache = do ->

View File

@ -345,7 +345,7 @@ Main =
<% } %>
).join '\n'
return unless errors
$.ajax '<%= meta.page %>errors', {},
$.ajax '<%= meta.page %>errors', null,
sync: true
form: $.formData
n: "<%= meta.name %> v#{g.VERSION}"

View File

@ -55,10 +55,10 @@ DeleteLink =
link = @
$.ajax $.id('delform').action.replace("/#{g.BOARD}/", "/#{post.board}/"),
withCredentials: true
onload: -> DeleteLink.load link, post, fileOnly, @response
onerror: -> DeleteLink.error link
,
cred: true
form: $.formData form
load: (link, post, fileOnly, html) ->
tmpDoc = d.implementation.createHTMLDocument ''

View File

@ -959,7 +959,8 @@ QR =
recaptcha_challenge_field: challenge
recaptcha_response_field: response
callbacks =
options =
withCredentials: true
onload: QR.response
onerror: ->
# Connection error, or
@ -973,8 +974,7 @@ QR =
Connection error. You may have been <a href=//www.4chan.org/banned target=_blank>banned</a>.
[<a href="https://github.com/MayhemYDG/4chan-x/wiki/FAQ#what-does-connection-error-you-may-have-been-banned-mean" target=_blank>FAQ</a>]
"""
opts =
cred: true
extra =
form: $.formData postData
upCallbacks:
onload: ->
@ -988,7 +988,7 @@ QR =
QR.req.progress = "#{Math.round e.loaded / e.total * 100}%"
QR.status()
QR.req = $.ajax $.id('postForm').parentNode.action, callbacks, opts
QR.req = $.ajax $.id('postForm').parentNode.action, options, extra
# Starting to upload might take some time.
# Provide some feedback that we're starting to submit.
QR.req.uploadStartTime = Date.now()