Extjs Utility Functions

extjs

How to break out of Ext.each?

return false;

Return false from inside your function.

If we have a JSON object, how can we serialize it into a JSON string?

var jsonString  = Ext.encode(myObject);

If we have a JSON string, how can we deserialize it into a JSON object?

var myObject = Ext.decode(jsonString);

If we have a string, how can we URL encode it?

var encodedString = Ext.urlEncode(rawString);

If we have a string, how can we URL decode it?

var decodedString = Ext.urlDecode(encodedString);

How to find the index of an element of an array?

var position = myArray.indexOf('Duck'); //0 is the first element, -1 is not found

How to remove an element from an array?

myArray.remove('Dog');

How to escape a string?

var newString = String.escape(myString);

How to trim a string?

x = x.trim();

What is String.format()?

var newString = String.format('You have {0} messages', msgCount);

How to convert a string to uppercase?

var newString = Ext.util.Format.uppercase(myString);

How to capitalize a string?

Ext.util.Format.capitalize();

How to convert a string to lowercase?

Ext.util.Format.lowercase();

How to remove script tags from a string?

Ext.util.Format.stripScripts()

How to convert newline to break?

Ext.util.Format.nl2br();

How to format currency?

Ext.util.Format.usMoney();

How to truncate a string with ellipsis?

var newString = Ext.util.Format.ellipsis(myString, 20);

How to extract a sub-string from a string given the starting index?

var newString = Ext.util.Format.substr(myString, 0, 5);

How to left pad a string?

var newString = Ext.util.Format.leftPad('23', 4, '0');

If we have a variable that is undefined, how can we assign it a default value?

Ext.num(myObject, 0); //provide a default value it's not a number

How can we constraint the value of a variable to a range?

var constrained = number1.constrain(25, 50); //force it in the range

How to provide default value for string?

var allowZeroLen = false;
Ext.value(myObject, 'default', allowZeroLen);

How to format a date?

var now = new Date();
var formatted = now.format('Y-m-d');

How to parse a date?

var aDate = Date.parseDate('2010-12-09', 'Y-m-d');

How to check if a date is within a certain range?

var inRange = now.between(lowDate, highDate);
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License