2014-05-18 18:51:05 -04:00
#jQuery for node, reimplementated for compatibility purposes. Poorly.
2015-04-10 19:19:27 -04:00
#Leaves out all the DOM stuff but allows ajax.
2014-05-18 18:51:05 -04:00
_ = require ' lodash '
request = require ' request '
2014-06-30 22:16:26 -04:00
Deferred = require ' JQDeferred '
2014-05-18 18:51:05 -04:00
module.exports = $ = (input) ->
console . log ' Ignored jQuery: ' , input if $ . _debug
append: (input)-> exports: ()->
# Non-standard jQuery stuff. Don't use outside of server.
$._cookies = request . jar ( )
$.when = Deferred . when
$.ajax = (options) ->
responded = false
url = options . url
if url . indexOf ( ' http ' )
url = ' / ' + url unless url [ 0 ] is ' / '
url = $ . _server + url
2014-06-30 22:16:26 -04:00
console . log ' Requesting: ' + JSON . stringify options if $ . _debug
console . log ' URL: ' + url if $ . _debug
2015-04-10 19:19:27 -04:00
if /db\/thang.type\/names/ . test url
url += ' ?_= ' + Math . random ( ) # Make sure that the ThangType names don't get cached, since response varies based on parameters in a way that apparently doesn't work in this hacky implementation.
2014-05-18 18:51:05 -04:00
deferred = Deferred ( )
request
url: url
jar: $ . _cookies
json: options . parse
method: options . type
2015-04-10 19:19:27 -04:00
body: options . data
2014-05-18 18:51:05 -04:00
, (error, response, body) ->
2014-06-30 22:16:26 -04:00
console . log ' HTTP Request: ' + JSON . stringify options if $ . _debug and not error
2014-05-18 18:51:05 -04:00
if responded
2014-06-30 22:16:26 -04:00
console . log ' \t ↳Already returned before. ' if $ . _debug
2014-05-18 18:51:05 -04:00
return
if ( error )
console . warn " \t ↳Returned: error: #{ error } "
deferred . reject ( error )
else
console . log " \t ↳Returned: statusCode #{ response . statusCode } : #{ if options . parse then JSON . stringify body else body } " if $ . _debug
deferred . resolve ( body , response , status: response . statusCode )
statusCode = response . statusCode if response ?
options . complete ( status: statusCode ) if options . complete ?
responded = true
deferred . promise ( ) . done ( options . success ) . fail ( options . error )
$.extend = (deep, into, from) ->
2015-04-10 19:19:27 -04:00
copy = _ . clone ( from , deep )
2014-05-18 18:51:05 -04:00
if into
_ . assign into , copy
copy = into
copy
$.isArray = (object) ->
_ . isArray object
$.isPlainObject = (object) ->
_ . isPlainObject object