/** * Request v1.1 * Copyright (C) 2012 AlfaCode @ support@alfacode.net * */ function objById( id ){ var obj = document.getElementById(id); if( obj == 'null' || obj == 'undefined' || obj === null ) return false return obj } (function(window){ var Request = { obj : null, self : null, action: null, loader : { className:'ajax-loader' }, setObj : function( obj ){ self = this; var id = typeof( obj ) == 'object' ? obj.id : obj; this.obj = objById( id ); /* View */ if( !this.obj ) this.obj = new View( obj ); this.obj = objById( this.obj.id + '_Body' ) ? objById( this.obj.id + '_Body' ) : this.obj; return this; }, $ : function( obj ){ this.setObj( obj ) return this; }, send : function(url, data, type ){ this.xhr(url, data, type ) }, submit : function(url, data, type ){ this.xhr(url, data, type ) }, get : function(url, data ){ this.xhr(url, data, 'get' ) }, post : function(url, data ){ this.xhr(url, data, 'post' ) }, put : function(url, data ){ this.xhr(url, data, 'put' ) }, xhr : function( url, data, type ){ this.didStartLoad(); this.jsHttpRequest(self.obj, url , data, type || 'POST' ) return this; }, jsHttpRequest : function( obj, url, data, type ){ var req = new JsHttpRequest(); var tar = obj; // New !!! req.open( type, url, true ); setTimeout(function() { req.send( data ); }, 10 ) req.onreadystatechange = function() { if (req.readyState == 4) { if (req.status == 200){ self.didFinishLoad() tar.innerHTML = req.responseText; self.postResponce( tar.childNodes ) }else{ self.didFailLoadWithError() tar.innerHTML = 'Error: ' + req.status + '
' + req.responseText } //RESET self.didStartLoad = function(){ self.displayLoader( self.obj ) } self.didFinishLoad = function(){ } self.didFailLoadWithError = function(){ } action = null } } }, displayLoader : function( obj ){ var LoaderBox = objById( obj.id + '_Loader' ); if( !LoaderBox ){ var Loader = document.createElement('div'); with(Loader){ id = obj.id+'_Loader'; className = this.loader.className; innerHTML = " "; } obj.appendChild(Loader); } return this }, didStartLoad: function(){ self.displayLoader( self.obj ) }, didFinishLoad: function(){}, didFailLoadWithError: function(){}, postResponce : function( responce ){ var script_id = 'app_js' nodes = responce var scripts = []; for ( var i = 0; nodes[i]; i++ ) { if ( scripts && nodes[i].nodeName.toLowerCase() === "script" && ( !nodes[i].type || nodes[i].type.toLowerCase() === "text/javascript" ) ) { scripts.push( nodes[i].parentNode ? nodes[i].parentNode.removeChild( nodes[i] ) : nodes[i] ); } } for ( script in scripts ){ var scr = document.getElementById( script_id ) if ( scr ) scr.parentNode.removeChild( scr ) var scr = document.createElement('script'); scr.id = script_id; scr.type = 'text/javascript'; scr.text = scripts[script].text || scripts[script].textContent || scripts[script].innerHTML || ""; if( scripts[script].getAttribute("src") ){ scr.src = scripts[script].src || ''; } document.body.appendChild( scr ); } } } window.Request = Request; } )(window);