UNPKG

khoaijs-ajax

Version:

![http://i.imgur.com/RmD50NR.jpg](http://i.imgur.com/RmD50NR.jpg)

3 lines (2 loc) 9.29 kB
!function(t,e){if("function"==typeof define&&define.amd){var r=["lodash","jquery","khoaijs","khoaijs-event-emitter","khoaijs-pre-options","khoaijs-task","khoaijs-priority"];define(r,function(r,i,s,o,n,a,p){var u=e(r,i,s,o,n,a,p);return s.Ajax=u,t.Ajax=u,u})}else{var i=e(t._,t.jQuery,t.Khoai,t.Khoai.EventEmitter||t.EventEmitter,t.Khoai.PreOptions||t.PreOptions,t.Khoai.Task||t.Task,t.Khoai.Priority||t.Priority);t.Khoai.Ajax=i,t.Ajax=i}}(this,function(t,e,r,i,s,o,n){"use strict";function a(e){i.call(this),this.options=s.get(m.AJAX_PRE_OPTIONS_NAME),this.jqXHR=null,this.requested=0,this.retry_time=0,this._last_options=null,this._is_retrying=!1,this._retry_timeout_id=null,this.response=null,this.error=null,this._is_response_meaning_failed=!1,t.isObject(e)&&this.option(e)}function p(e){var r=o.apply(e,this.options.response_tasks);if(r.error)return this._is_response_meaning_failed=!0,this.error=r.error,void(this.isRetryable()||this.emitEvent("fail",r.error.message,r.error.code));this.response=r.data,this.emitEvent("done",t.clone(r.data))}function u(t,e,r){t.hasOwnProperty("responseJSON")&&(this.response=t.responseJSON),this.error=a.beautifyError(arguments),this.isRetryable()||this.isAborted()||this.emitEvent("fail",this.error.message,this.error.code)}function h(t){t._last_options=null,t._is_retrying=!1,t.emitEvent("always",t.error,t.response)}function l(t,e){var r=this;if(this.isAborted())this.emitEvent("aborted");else if(this.isRetryable())return this.isRetrying()&&this.emitEvent("retry_complete",this.retry_time,this.isLastRetryTime(),t,e),this._is_retrying=!0,void(this.options.retry_delay?this._retry_timeout_id=setTimeout(function(){r.request()},this.options.retry_delay):this.request());h(this)}function c(e,r){var i,s=t.extend({},e.options,t.isObject(r)?r:{});if((s.hasOwnProperty("success")||s.hasOwnProperty("done"))&&(e.removeListener("success_listeners_from_options"),e.addListener("done",t.values(t.pick(s,["success","done"])),{priority:n.PRIORITY_HIGHEST,key:"success_listeners_from_options"})),(s.hasOwnProperty("error")||s.hasOwnProperty("fail"))&&(e.removeListener("error_listeners_from_options"),e.addListener("fail",t.values(t.pick(s,["error","fail"])),{priority:n.PRIORITY_HIGHEST,key:"error_listeners_from_options"})),(s.hasOwnProperty("complete")||s.hasOwnProperty("always"))&&(e.removeListener("complete_listeners_from_options"),e.addListener("always",t.values(t.pick(s,["complete","always"])),{priority:n.PRIORITY_HIGHEST,key:"complete_listeners_from_options"})),s.hasOwnProperty("beforeSend")&&(i=s.beforeSend),s=t.omit(s,["success","done","error","fail","complete","always","beforeSend"]),s.done=p.bind(e),s.fail=u.bind(e),s.always=l.bind(e),s.beforeSend=function(r,o){var n=!0;return e.option("auto_abort")&&e.isRequesting()&&e.abort(),t.isFunction(i)&&!e.isRetrying()&&(n=i.apply(e,[r,o])),t.isObject(s.data)||(s.data={}),!1!==n&&(e.requested++,e.error=null,e._is_response_meaning_failed=!1,e.response=null,e.responses=null,e.isRetrying()?(e.retry_time++,e.emitEvent("retry")):(e.retry_time=0,e.emitEvent("request"))),n},!t.isEmpty(s.data_tasks)){var a=o.apply(t.clone(s.data),s.data_tasks);if(!a.data)return this.error=a.error,!1;s.data=a.data}return t.omit(s,["response_tasks","data_tasks","auto_abort","retry","retry_delay","is_continue"])}function d(r,i){var s;return(1==arguments.length||r.isRetrying())&&t.isObject(r._last_options)?s=r._last_options:(s=c(r,i),!1!==s?r._last_options=s:(r.emitEvent("fail",r.error.message,r.error.code),h(r))),s&&(r.jqXHR=e.ajax(t.omit(s,["done","fail","always"])).done(s.done).fail(s.fail).always(s.always)),r}function y(t,e,i,s,o,n){var a,p={url:i,method:(e+"").toUpperCase()};return a=r.util.optionalArgs(Array.prototype.slice.call(arguments,3),["data","callback","dataType"],{data:["string","Object"],callback:"Function",dataType:"string"}),a.data&&(p.data=a.data),a.callback&&(p.done=a.callback),a.dataType&&(p.dataType=a.dataType),d(t,p)}function _(t,r,i){switch(r=e(r),i){case"append":r.append(t);break;case"prepend":r.prepend(t);break;default:r.html(t)}}var f,m,b={204:"Server has received the request but there is no information to send back",400:"The request had bad syntax or was inherently impossible to be satisfied",401:"The parameter to this message gives a specification of authorization schemes which are acceptable",403:"The request is for something forbidden",404:"The server has not found anything matching the URI given",405:"Method not allowed",406:"Not acceptable",408:"Request timeout",413:"Payload too large",414:"URI too long",429:"Too many requests",431:"Request header fields too large",500:"The server encountered an unexpected condition which prevented it from fulfilling the request",501:"The server does not support the facility required",parser_error:"Parse response failed",aborted:"Manual abort request"};m={AJAX_PRE_OPTIONS_NAME:"Khoai.Ajax",AJAX_ABORTED:"aborted",AJAX_TIMEOUT:"timeout",AJAX_PARSER_ERROR:"parser_error",AJAX_SERVER_ERROR:500,AJAX_FORBIDDEN:403,AJAX_NOT_FOUND:404},r.util.inherit(a,i);for(f in m)m.hasOwnProperty(f)&&Object.defineProperty(a,f,{enumerable:!0,value:m[f]});return s.define(m.AJAX_PRE_OPTIONS_NAME,{response_tasks:[],data_tasks:{},auto_abort:!0,retry:0,retry_delay:0,is_continue:!0}),a.globalOption=function(t){s.update(m.AJAX_PRE_OPTIONS_NAME,t)},a.beautifyError=function(t){var e=t[0],r=t[1],i=t[2],s={code:"",message:"Ajax error"};switch(r){case"parsererror":s.code=m.AJAX_PARSER_ERROR;break;case"abort":s.code=m.AJAX_ABORTED;break;default:s.code=e.status}return b.hasOwnProperty(s.code)?s.message=b[s.code]:s.message=i,s},a.prototype.option=function(e,i){return this.options=r.util.setup.apply(null,[this.options].concat(t.toArray(arguments))),this},a.prototype.data=function(t){return this.options.data=t,this},a.prototype.addData=function(e,r){var i;return this.options.data||(this.options.data={}),t.isObject(arguments[0])?i=t.extend({},e):(i={},arguments.length<2?i[e+""]="true":i[e]=r),t.isObject(this.options.data)?t.extend(this.options.data,i):t.isString(this.options.data)&&(i=t.map(i,function(t,e){return e+"="+t}),this.options.data.length?this.options.data+="&"+i:this.options.data=i),this},a.prototype.done=function(e){return this.on("done",e),this.isSuccess()&&e.apply(this,[t.clone(this.response)]),this},a.prototype.fail=function(t){return this.on("fail",t),this.isFailed()&&t.apply(this,[this.error.message,this.error.code]),this},a.prototype.always=function(t){return this.on("always",t),this.isDone()&&t.apply(this,[this.error,this.response]),this},a.prototype.status=function(){return!(!t.isObject(this.jqXHR)||!this.jqXHR.hasOwnProperty("readyState"))&&this.jqXHR.readyState},a.prototype.isReady=function(){var t=this.status();return!1!==t&&0===t},a.prototype.isRequesting=function(){var t=this.status();return!1!==t&&t>=1&&t<=3},a.prototype.isRetrying=function(){return Boolean(this._is_retrying)},a.prototype.isDone=function(){return!this.isRetrying()&&4===this.status()},a.prototype.isFailed=function(){return this.isDone()&&!t.isEmpty(this.error)},a.prototype.isResponseMeaningFailed=function(){return this.isFailed()&&this._is_response_meaning_failed},a.prototype.isSuccess=function(){return this.isDone()&&t.isEmpty(this.error)},a.prototype.isAborted=function(){return this.error&&this.error.code===m.AJAX_ABORTED},a.prototype.isLastRetryTime=function(){return this.isRetrying()&&this.options.retry&&this.retry_time>=parseInt(this.options.retry)},a.prototype.isRetryable=function(){if(!t.isEmpty(this.error)&&!this.isAborted()&&!this.isResponseMeaningFailed()&&this.options.retry&&this.retry_time<parseInt(this.options.retry)){return t.isFunction(this.options.is_continue)?this.options.is_continue.bind(this)(t.clone(this.error)):Boolean(this.options.is_continue)}return!1},a.prototype.request=function(t){return d(this,t)},a.prototype.abort=function(){this.isRequesting()&&this.jqXHR.abort?this.jqXHR.abort():this.isRetrying()&&(clearTimeout(this._retry_timeout_id),this.emitEvent("aborted"),h(this))},["get","post","put","delete"].forEach(function(t){a.prototype[t]=function(e,r,i,s){return y.apply(null,[this,t.toUpperCase()].concat(Array.prototype.slice.call(arguments,0)))}}),["get","post"].forEach(function(t){a.prototype[t+"JSON"]=function(t,e,i){var s=r.util.optionalArgs(Array.prototype.slice.call(arguments,0,3),["url","data","callback"],{url:"string",data:["string","Object"],callback:"Function"}),o={dataType:"json"};return s.url&&(o.url=s.url),s.data&&(o.data=s.data),s.callback&&(o.done=s.callback),this.request(o)}}),["get","post","put","delete","getJSON","postJSON"].forEach(function(t){a[t]=function(e,r,i,s){var o=new a;return o[t].apply(o,Array.prototype.slice.call(arguments,0)),o}}),a.prototype.query=function(t){var e={method:"GET"};return t&&(e.data=t),this.request(e)},a.prototype.send=function(t){var e={method:"POST"};return t&&(e.data=t),this.request(e)},a.use=function(t,e){return new a(s.get(t,e))},a.load=function(e,r,i){var s=new a;return t.isObject(i)||(i={}),i=t.extend({error_content:"",apply_type:"replace"},i,{url:e}),s.option(i),s.done(function(t){_(t,r,i.apply_type)}).fail(function(e,o){var n="";i.error_content&&(n=t.isFunction(i.error_content)?i.error_content(s,e,o):i.error_content+""),_(n,r,i.apply_type)}),s.request(),s},a}); //# sourceMappingURL=ajax.min.js.map