Results 1 to 5 of 5

Thread: Manage all the Ajax requests in the same way

  1. #1
    Sencha User
    Join Date
    May 2015
    Posts
    44
    Vote Rating
    0
      0  

    Default Manage all the Ajax requests in the same way

    Hi, i need to perform some ajax requests and, once the response is retrieved, i want to send it to the main controller that shows a window with message retrieved from the server.

    Actually i manage it in the "success" and "failure" parameters of the request method in EACH ajax call, but this cause a redundant code.
    Since i need to manage alla ajax response in the same manners, i want to create a "default" action for all ajax requests, that automatically redirects every response to the controller. How i can do that?

  2. #2
    Sencha Premium User alexander.urban's Avatar
    Join Date
    Nov 2013
    Posts
    324
    Answers
    9
    Vote Rating
    51
      0  

    Default

    I have made an Ext.Ajax.requestAuthn function that wraps around the original Ext.Ajax.request function. You could do similar:

    Code:
    		Ext.Ajax.requestAuthn = function(options)		{
    			var success = options.success,
    				callback = options.callback,
    				error = options.error;
    			if(Ext.isFunction(success)) options.success = function(response) { if(isLoggedIn(response)) success(response); }
    			if(Ext.isFunction(callback)) options.callback = function(response) { if(isLoggedIn(response)) callback(response); }
    			if(Ext.isFunction(error)) options.error = function(response) { if(isLoggedIn(response)) error(response); }
    			Ext.Ajax.request(options);
    		}

  3. #3
    Sencha User
    Join Date
    May 2015
    Posts
    44
    Vote Rating
    0
      0  

    Default

    Hum. Can you write an example of how you use it?

  4. #4
    Sencha Premium User alexander.urban's Avatar
    Join Date
    Nov 2013
    Posts
    324
    Answers
    9
    Vote Rating
    51
      0  

    Default

    You use it the same way you would use Ext.Ajax.request. What my wrapper function does is add a call to a function (isLoggedIn) to all callbacks.

    When I made it, I already had like 200 calls to Ext.Ajax.request in my source code.

    What I did was search and replace all Ext.Ajax.request calls to Ext.Ajax.requestAuthn, and everything worked the same as before, just with the little call to the isLoggedIn function after every Ext.Ajax.request, in which I check whether the response indicates that the user is no longer logged in and triggers a reauthentication if he isn't.

  5. #5
    Sencha User
    Join Date
    May 2015
    Posts
    44
    Vote Rating
    0
      0  

    Default

    Can i save it in a standalone js file? And if yes, how i can include it inside another file, for example a controller, in order to use it?

Similar Threads

  1. How to manage HttpProxy' Stores requests
    By santosr666 in forum Ext 3.x: Help & Discussion
    Replies: 3
    Last Post: 10 May 2013, 4:47 AM
  2. Unify Ajax requests and Model Proxy requests.
    By iamcam in forum Sencha Touch 1.x: Bugs
    Replies: 0
    Last Post: 29 Jun 2011, 9:55 PM
  3. Managing HTTP Requests on Stores, TreeLoaders and Ajax Requests
    By j-joey in forum Ext 3.x: Help & Discussion
    Replies: 2
    Last Post: 14 Jun 2010, 11:32 PM
  4. Need two or more Ajax requests???
    By finderetf in forum Ext 3.x: Help & Discussion
    Replies: 4
    Last Post: 13 Nov 2009, 10:10 AM
  5. Just an opinion: How to manage huge post requests?
    By SchattenMann in forum Ext 3.x: Help & Discussion
    Replies: 8
    Last Post: 24 Aug 2009, 7:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •