-
9 Aug 2007 8:01 AM #1
Synchronous Ajax Call
Synchronous Ajax Call
This is in response to this thread.
I agree that it is ideal to use async requests as much as possible, even if it requires refactoring much of your other code to make it work that way. However, there is value in having the option to make a synchronous call if one should wish, and there are situations where it may be desired. Not to mention that many people have requested this functionality.
Anyway, here it is:
Example:Code:Ext.lib.Ajax.request = function(method, uri, cb, data, options) { if (options) { var hs = options.headers; if (hs) { for (var h in hs) { if (hs.hasOwnProperty(h)) { this.initHeader(h, hs[h], false); } } } if (options.xmlData) { this.initHeader('Content-Type', 'text/xml', false); method = 'POST'; data = options.xmlData; } var async = (options.async === false) ? false : true; } return this.asyncRequest(method, uri, cb, data, async); }; Ext.lib.Ajax.asyncRequest = function(method, uri, callback, postData, async) { var o = this.getConnectionObject(); if (!o) { return null; } else { o.conn.open(method, uri, async); if (this.useDefaultXhrHeader) { if (!this.defaultHeaders['X-Requested-With']) { this.initHeader('X-Requested-With', this.defaultXhrHeader, true); } } if (postData && this.useDefaultHeader) { this.initHeader('Content-Type', this.defaultPostHeader); } if (this.hasDefaultHeaders || this.hasHeaders) { this.setHeader(o); } this.handleReadyState(o, callback); o.conn.send(postData || null); return o; } };
Code:Ext.Ajax.request({ url: 'some_page.php', method: 'POST', success: success, failure: failure, scope: this, params: {foo: 'bar'}, async: false }); // or directly ... Ext.lib.Ajax.request('POST', url, callback, params, {async: false});
DISCLAIMER: This is ONLY for use with ext-base.js.
-
23 Apr 2008 10:43 AM #2
Update
Update
Hi, I needed to make some synchronous requests using ExtJS and tried the solution posted in the previous post, however it didn't work for me, even thought the XMLHttpRequest was synchronous, Ext.lib.Ajax was handling the response as if it were async (using setInterval), that caused the Ext.lib.Ajax.request function to return just before the XMLHttpRequest was performed, giving an async feeling. So I just updated the code previously posted here that creates a syncRequest function on Ext.lib.Ajax to handle synchronous requests.
Usage is the same as described on previous post.
Code:/* -*-js2-*- */ Ext.lib.Ajax.request = function(method, uri, cb, data, options) { if(options){ var hs = options.headers; if(hs){ for(var h in hs){ if(hs.hasOwnProperty(h)){ this.initHeader(h, hs[h], false); } } } if(options.xmlData){ this.initHeader('Content-Type', 'text/xml', false); method = 'POST'; data = options.xmlData; }else if(options.jsonData){ this.initHeader('Content-Type', 'text/javascript', false); method = 'POST'; data = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData; } if (options.async == false) { return this.syncRequest(method, uri, cb, data); } } return this.asyncRequest(method, uri, cb, data); }; Ext.lib.Ajax.syncRequest = function(method, uri, callback, postData) { var o = this.getConnectionObject(); if (!o) { return null; } else { o.conn.open(method, uri, false); if (this.useDefaultXhrHeader) { if (!this.defaultHeaders['X-Requested-With']) { this.initHeader('X-Requested-With', this.defaultXhrHeader, true); } } if(postData && this.useDefaultHeader){ this.initHeader('Content-Type', this.defaultPostHeader); } if (this.hasDefaultHeaders || this.hasHeaders) { this.setHeader(o); } o.conn.send(postData || null); this.handleTransactionResponse(o, callback); return o; } };
-
18 May 2008 8:05 AM #3
Hi,
I a mvery interested by this code, but i would like to know where i have to paste these methods in the extjs code ?
Thanks
-
19 May 2008 7:28 AM #4
You don't have to patch the source to use it, just place the code on a sync.js file, and load it in your page just after loading ext.
-
29 May 2008 7:57 PM #5
There is a less-intrusive way to do this too:
Code:Ext.lib.Ajax.originalRequest = Ext.lib.Ajax.request; Ext.lib.Ajax.originalAsyncRequest = Ext.lib.Ajax.asyncRequest; Ext.lib.Ajax.request = function(method, uri, cb, data, options) { if ( options.sync === true ) { this.asyncRequest = function() { this.asyncRequest = Ext.lib.Ajax.originalAsyncRequest; // we don't really need this ... this.syncRequest.apply(this,arguments); // refer to previous posts for code to this } } else { this.asyncRequest = Ext.lib.Ajax.originalAsyncRequest; // reset this - just in case we had some errors executing the body of request() } this.originalRequest.apply(this, arguments); };
-
15 Jul 2008 4:03 PM #6
Dear All:
Thanks for all of your contribution,it matches my requirement
But I don't know if you found another problem caused by this "patch" that:
in IE,the "Ext.MessageBox.show" no longer showen when I add faisalv's & Victor Hugo Borja 's js code ,but "Ext.MessageBox.show" shows in firefox.
Did anyone also had such problem? and how to solve it?
Thanks for any reply
Regards
john
-
21 Oct 2008 12:20 AM #7
Update for Ext 2.2
Update for Ext 2.2
I've had a go at updating this to match the Ext2.2 code.
The only bit I wasn't too sure about was the setting of headers.
E.g. the code...
This doesn't look right to me. I always thought that a hyphen was an invalid character for property names because it gets interpreted as a subtraction sign.Code:hs['Content-Type']
i.e.is the same asCode:hs.Content-Type
which is obvioulsy wrong. Maybe you can always get away with it if you only access the property via the square bracket notation?Code:(hs.Content)-Type
Anyway, here is the code.
Max
HTML Code:/** Adding a synchronous request to the Ext asynchronous only mode of operation. History: coded from Ext 2.2. Additional configs. @param {Object} options @config {Mixed} [sync] include this for a synchronous request */ Ext.lib.Ajax.request = function(method, uri, cb, data, options) { if(options){ var hs = options.headers; if(hs){ for(var h in hs){ if(hs.hasOwnProperty(h)){ this.initHeader(h, hs[h], false); } } } if(options.xmlData){ if (!hs || !hs['Content-Type']){ this.initHeader('Content-Type', 'text/xml', false); } method = (method ? method : (options.method ? options.method : 'POST')); data = options.xmlData; }else if(options.jsonData){ if (!hs || !hs['Content-Type']){ this.initHeader('Content-Type', 'application/json', false); } method = (method ? method : (options.method ? options.method : 'POST')); data = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData; } } return this["sync" in options ? "syncRequest" : "asyncRequest"](method, uri, cb, data); }; /** Synchronous request. @param {Object} method @param {Object} uri @param {Object} callback @param {Object} postData */ Ext.lib.Ajax.syncRequest = function(method, uri, callback, postData) { var o = this.getConnectionObject(); if (!o) { return null; } else { o.conn.open(method, uri, false); if (this.useDefaultXhrHeader) { if (!this.defaultHeaders['X-Requested-With']) { this.initHeader('X-Requested-With', this.defaultXhrHeader, true); } } if(postData && this.useDefaultHeader && (!this.hasHeaders || !this.headers['Content-Type'])){ this.initHeader('Content-Type', this.defaultPostHeader); } if (this.hasDefaultHeaders || this.hasHeaders) { this.setHeader(o); } o.conn.send(postData || null); this.handleTransactionResponse(o, callback); return o; } };
-
21 Oct 2008 12:41 AM #8Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 43
-
21 Oct 2008 2:32 AM #9
Can anyone provide a use case for synchronous requests?
Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
21 Oct 2008 4:45 AM #10
Ext-basex
Ext-basex
Ext-basex requires a Commercial licence. I don't see the point in buying one just to be able to do a synchronous request which can be coded in a few lines.
Max


Reply With Quote