/* Library: Gainsborough - web client framework Component: AJAX object v2.0 Hextra Digital http://www.hextra-digital.com */ /* ajaxobject2 basic implementation - used in ajax requests can be expanded by using ajaxobject2.prototype */ var eAjaxModes = { Get: 0, Post: 1 }; function ajaxobject2() {} ajaxobject2.prototype = { eCodes: { Ok: 0, NoXHR: 1, Exception: 2, Error: 3, NoStatus: 4, Connecting: 5, Processing: 6, Starting: 7 }, create: function( _this, _callback, _mode, _url, _parameters ) { this.thisPtr = _this; this.mode = _mode; this.url = _url; this.callback = _callback; this.parameters = _parameters; this.retry = 0; this.timeout = null; this.createXHR(); }, enableAutoRetry: function( _interval ) { this.retry = _interval; }, checkRetry: function() { this.clearRetry(); if( this.retry > 0 ){ var thisPtr = this; this.timeout = setTimeout( function(){ thisPtr.request(); }, this.retry ); } }, destroy: function() { clearRetry(); this.http_request = null; return null; }, clearRetry: function() { if( this.timeout ) { clearTimeout( this.timeout ); this.timeout = null; } }, createXHR: function() { if( window.XMLHttpRequest ) { // Mozilla, Safari,... this.http_request = new XMLHttpRequest(); if( this.http_request ) { if( this.http_request.overrideMimeType ) { this.http_request.overrideMimeType( 'text/xml' ); } } } else if( window.ActiveXObject ) { // MSIE try { this.http_request = new ActiveXObject( 'Msxml2.XMLHTTP' ); } catch( e ) { try { this.http_request = new ActiveXObject( 'Microsoft.XMLHTTP' ); } catch( e ) { this.notify( this.eCodes.Exception, e.description ); } } } if( !this.http_request ) { this.notify( this.eCodes.NoXHR, '' ); return false; } if( this.callback ) { var thisObj = this; this.http_request.onreadystatechange = function() { thisObj.process(); }; } }, request: function() { this.clearRetry(); this.notify( this.eCodes.Starting, '' ); switch( this.mode ) { case eAjaxModes.Post: { // asynchronous HTTP POST this.notify( this.eCodes.Connecting, '' ); this.http_request.open( 'POST', this.url, true ); this.http_request.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' ); this.http_request.setRequestHeader( 'Content-length', this.parameters.length ); this.http_request.setRequestHeader( 'Connection', 'close' ); this.http_request.send( this.parameters ); break; } case eAjaxModes.Get: { // asynchronous HTTP GET this.notify( this.eCodes.Connecting, '' ); this.http_request.open( 'GET', this.url, true ); this.http_request.send( null ); break; } default: { } } }, process: function() { this.notify( this.eCodes.Processing, '' ); if( this.http_request ) { if( this.http_request.readyState == 4 ) { // once is completed try { if( this.http_request.status ) { if( this.http_request.status == 200 || this.http_request.status == 0 ) { // OK if( this.callback ) { try { this.notify( this.eCodes.Ok, '' ); this.callback( this ); } catch( e ) { this.notify( this.eCodes.Exception, e.description ); } } else { return this; } } else { // 404, 500, etc. this.notify( this.eCodes.Error, '' ); } } else this.notify( this.eCodes.NoStatus, '' ); } catch( e ) { this.notify( this.eCodes.Exception, e.description ); } //this.destroy(); } } else { this.notify( eCodes.Exception, e.description ); } }, notify: function( _code, _text ) { switch( _code ){ case this.eCodes.Exception:{ this.checkRetry(); break; } case this.eCodes.Error:{ this.checkRetry(); break; } } } };