Tuesday 12 March 2013

Ext.isEmpty() in Sencha Touch 2 Explained

I am from PHP Background, Checking for variable empty in PHP is straight forward using empty() function. I am searching for similar function in Sencha Touch 2, finally i got it.

 Ext.isEmpty() returns true if the variable is empty, otherwise returns false. The variable is deemed to be empty if it is either:

 * null
 * undefined
 * zero-length array
 * zero-length string (Unless the allowEmptyString parameter is set to true)

 This method accept 2 parameters

 1) value : the value/variable to test
 2) allowEmptyString  : true to allow empty strings (defaults to false)

 Example 1: (Using null check)

 var data = null;
 if(Ext.isEmpty(data)){
    console.log('variable is empty');
 }else{
    console.log('variable is not empty');
 }

 Output: variable is empty

 Example 2: (Using zero-length array check)

var data = [];
 if(Ext.isEmpty(data)){
    console.log('variable is empty');
 }else{
    console.log('variable is not empty');
 }

 Output: variable is empty

Example 3: (Using zero-length string check)

var data = '';
 if(Ext.isEmpty(data)){
    console.log('variable is empty');
 }else{
    console.log('variable is not empty');
 }

 Output: variable is empty

 Hope, you enjoyed this Post.

No comments:

Post a Comment