3vot-model
Version:
3VOT Model based on SpineJS
46 lines (35 loc) • 1.24 kB
text/coffeescript
superagent = require("superagent")
AjaxUtils = require("./ajax_utils")
class AjaxRequest
=
end: ->
= true;
= (callback) ->
if
= false
try
do callback
catch e
throw e
finally
= true
else
do callback
=
get: (params, options) -> AjaxRequest.executeRequest("get",params, options)
post: (params, options) -> AjaxRequest.executeRequest("post",params, options)
put: (params, options) -> AjaxRequest.executeRequest("put",params, options)
del: (params, options) -> AjaxRequest.executeRequest("del",params, options)
= (type, params, options ) ->
if == false then return
request = superagent[type](options.url)
.type('json')
.set('X-Requested-With','XMLHttpRequest')
request.withCredentials?()
if options.error then request.on("error", options.error)
if params.query then request = request.query( params.query )
if params.data
params.data = JSON.stringify(params.data) if typeof params.data isnt 'string'
request = request.send( params.data )
return request
module.exports = AjaxRequest