Saturday 15 June 2013

Top five String method that helps in your Sencha Touch 2 Apps

Today, i am going to list you the top five most widely used String methods available in Sencha Touch 2. Following are those.

1) eclipsis

    This method will truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length.

It accepts the following parameters
    string : The string to truncate.
    length : The maximum length to allow before truncating.
    word :   true to try to find a common word break.

Example
var inputStr = "Sencha Touch Rocks";
var outputStr = eclipsis(inputStr,12);

Output: Sencha Touch...

Usage:
  • This method will be used if you want to display short description about something and by cliking 'read more', you want to display full page description
  • If you don't want to display the whole content inorder to reduce the page size
2) trim

    This method will Trims whitespace from either end of a string, leaving spaces within the string intact.

It accept the following parameters
    string: the string to remove space

Example
var inputStr = " Sencha Touch Rocks ";
var outputStr = trim(inputStr);

Output: Sencha Touch Rocks

Usage
  • If you want to remove whitespaces from the HTML Form Post Data
  • If you don't need to insert data into WebSQL with Whiteapces
3) htmlEncode

   This method will convert certain characters (&, <, >, and ") to their HTML character equivalents for literal display in web pages.

  It accepts the following parameters
        string: the string to encode

Example
var inputStr = "<b>Sencha Touch</b> & Rocks";
var outputStr = htmlEncode(inputStr);

Output: &lt;b&gt;Sencha Touch&lt;b&gt; &amp; Rocks
Usage:
  •    If you want to encode the HTML Form Data inorder to insert/update record in WebSQL 
  •    If you want to avoid HTML Tags getting applied for any content displayed in your Mobile Web Page
4) htmlDecode

   This method will convert certain characters (&, <, >, and ") from their HTML character equivalents..

  It accepts the following parameters
       string: the string to decode

Example
var inputStr = "&lt;b&gt;Sencha Touch&lt;b&gt; &amp; Rocks";
var outputStr = htmlEncode(inputStr);

Output: <b>Sencha Touch</b> & Rocks
Usage:
  •    If you want to decode the encoded data inorder to display in the HTML Edit Form 
5)  ???

  I will leave the 5th option to you readers. Please let me know your favorite string method in Sencha Touch 2 through comments .

Hope, you enjoyed this Post.

No comments:

Post a Comment