""

About

Posts by :

Ext.Ajax: Creating global listeners

I started developing an application using ExtJs (of course) and at some point, after writing thousands of lines of code, having hundreds of ajax calls, it occurred to me that I want to perform an action against every ajax call.

So, the requirements were:
1. If user action has initiated an Ajax call but user session is expired (server returns 401 in this case), UI should show an alert. On this alert when user click on “OK” it should redirect user to login page.
2. It must also suppress any defined ‘failure’ handler for the corresponding ajax call.

Changing all ajax calls and adding appropriate conditions to ‘failure’ functions was going to be a tedious job. I came across a way to add global listeners which will be applicable for all ajax calls made through the application. I was having a js file which was being included in all views, so I added following code in that file:

Ext.Ajax.on('requestexception', function (conn, response, options) {
 
    if (response.status === 401) {
      Ext.Msg.alert(
        "Login session expired",
        "Your log-in session has expired", 
        function(btn, text){
            window.location = '/login';
        }
      );
    }
    return false;
});

This solved (1) above but the ‘failure’ defined for the ajax call was also getting executed. I found no way to purge those listeners (I tried purgeListeners() and abort()). With some further investigation I found a way to redefine the ‘failure’ function. So, the code looked like:

 Ext.Ajax.on('requestexception', function (conn, response, options) {
    if (response.status === 401){
      options.failure = function(){/*do nothing*/};
      Ext.Msg.alert(
        "Login session expired", 
        "Your log-in session has expired.", 
        function(btn, text){
            window.location = '/login';
        }
      );
    }
    return false;
});

This solved (2) above.

So far I am happy with this solution. Why? Because it solved the purpose :)

Hope it will be helpful for you if you got stuck in similar scenario.

Ref. http://docs.sencha.com/ext-js/3-4/#!/api/Ext.Ajax-event-requestexception

Regular Expressions in MySQL

Regular expressions (regex) are known to solve complex comparison problems. When it comes to Sql, numbers of developers don’t even know MySQL supports regular expressions. Let us directly jump to a couple of examples:
 

SELECT name FROM students WHERE name REGEXP  ‘^(A|E|I|O|U)’

Above SQL will retrieve students with name starting with character ‘A’, ‘E’, ‘I’, ‘O’ or ‘U’

 
SELECT name FROM students WHERE hobbies REGEXP  ‘[[:<:]]badminton[[:>:]]’

Above SQL will retrieve students having a word ‘badminton’ in `hobbies`

 

MYSQL METACHARACTERS:

  • . match any character 
  • ? match zero or one
  • * match zero or more
  • + match one or more
  • ^ beginning of line
  • $ end of line
  • {n} match n times
  • {m,n} match m through n times
  • {n,} match n or more times
  • [abc] match one of enclosed chars
  • [^xyz] match any char not enclosed
  • | separates alternatives
  • [:class:] match a character class
    • [:alpha:] for letters
    • [:space:] for whitespace
    • [:punct:] for punctuation
    • [:upper:] for upper case letters
  • [[:<:]] match beginning of words
  • [[:>:]] match ending of words

 

CAUTION:

  1. REGEXP operator works in bit-wise fashion. It means it may produce unexpected results while being used against multi-byte character set data.
  2. Queries with REGEXP can’t use indexes and may slow down the performance.
  3. MySQL supports POSIX regular expressions which lack negative lookaheads.

 

CONCLUSION:

While MySQL has extensive regex support and can be a handy and powerful tool, it has some downsides and can affect the SQL performance causing more problems than it can solve. So, if results can be achieved with LIKE or other string function, REGEXP must be avoided.

 
happy coding…