Sencha touch – manage ajax error

I’m developing a sencha touch app and I need to manage errors of every ajax call! The most simple solution consist of subscribe to every call and manage response but it’s very repetitive and I don’t like it!

The solution I choose is to extend base class of Connection and override response method.

In the launch method of app.js I added this code:

Ext.define('Ext.data.myConnection', {
override: 'Ext.data.Connection',

onComplete : function(request) {
var response = this.callParent([request]);

if(!request.options.disableLoginRedirect){
if(response.status != 200){
         var extraMsg = 'response code: ' + response.status;
         Ext.callback(Ext.goToLogin, this, [extraMsg]);
       }else if(response.responseText){
         try{
           //parse json resposne
           var resObj = Ext.JSON.decode(response.responseText);
           if(resObj && !resObj.success &&
             resObj.code === Ext.errorCodes.NOT_LOGGED){
             Ext.callback(Ext.goToLogin, this, ['Not logged!']););
           }
         }catch(e){
       }
     }
   }
   return response;
 }
});

With this code every ajax call will be verified automatically.

In case of you want avoid this behavior for some request it will enought add ‘disableLoginRedirect=true’ in the request options!

Ext.Ajax.request({
  url: requestUrl,
  disableLoginRedirect : true, //this options!!!!
  ....
  success:
   ...
  },
  failure: function(response, opts) {
   ...
  }
});

If you are using JSONP the class to modify is:

Ext.define('Ext.data.proxy.myJsonP', {
    override: 'Ext.data.proxy.JsonP',

    createRequestCallback: function(request, operation, callback, scope) {
        var jsonPme = this;
        
        return function(success, response, errorType) {
            delete jsonPme.lastRequest;
            //console.log('jsonp callback');

            if(success && response){                  
                if(!response.success && response.code === Ext.errorCodes.NOT_LOGGED && (!request.options || !request.options.disableLoginRedirect)){
                    var extraMsg = 'not logged!';
                    Ext.callback(Ext.goToLogin, this, [extraMsg]);
                }else{
                    jsonPme.processResponse(success, operation, request, response, callback, scope);
                }
            }
        };
    }
});
Annunci
Tagged with: , , , , , , , , , , ,
Pubblicato su Ext, ExtJs, Sencha
2 comments on “Sencha touch – manage ajax error
  1. giulioroggero ha detto:

    Great post Carlo!

  2. […] via Sencha touch – manage ajax error. […]

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: