UNPKG

3vot-model

Version:

3VOT Model based on SpineJS

80 lines (67 loc) 1.96 kB
(function() { var AjaxRequest, AjaxUtils, superagent; superagent = require("superagent"); AjaxUtils = require("./ajax_utils"); AjaxRequest = (function() { function AjaxRequest() {} AjaxRequest.promise = { end: function() {} }; AjaxRequest.enabled = true; AjaxRequest.disable = function(callback) { var e; if (this.enabled) { this.enabled = false; try { return callback(); } catch (_error) { e = _error; throw e; } finally { this.enabled = true; } } else { return callback(); } }; AjaxRequest.queueRequest = { get: function(params, options) { return AjaxRequest.executeRequest("get", params, options); }, post: function(params, options) { return AjaxRequest.executeRequest("post", params, options); }, put: function(params, options) { return AjaxRequest.executeRequest("put", params, options); }, del: function(params, options) { return AjaxRequest.executeRequest("del", params, options); } }; AjaxRequest.executeRequest = function(type, params, options) { var request; if (this.enabled === false) { return this.promise; } request = superagent[type](options.url).type('json').set('X-Requested-With', 'XMLHttpRequest'); if (typeof request.withCredentials === "function") { request.withCredentials(); } if (options.error) { request.on("error", options.error); } if (params.query) { request = request.query(params.query); } if (params.data) { if (typeof params.data !== 'string') { params.data = JSON.stringify(params.data); } request = request.send(params.data); } return request; }; return AjaxRequest; })(); module.exports = AjaxRequest; }).call(this);