Ext provides a series of general utility functions for working with Javascript and JSON. They vary in their functionality, but all of them aim to make your life as the developer easier.
var person = {
name: 'John Smith',
age: 30
};
Ext.apply(person, {
hobby: 'Coding',
city: 'London'
}); // person literal now contains hobby and city as well
var person = {
name: 'John Smith',
age: 30,
hobby: 'Rock Band'
};
Ext.applyIf(person, {
hobby: 'Coding',
city: 'London'
}); // hobby is not copied over
These methods are useful for transforming JSON data to a format that can be transmitted as part of a GET string and vice versa.
var params = {
foo: 'value1',
bar: 100
};
var s = Ext.encode(params); // s is now foo=value1&bar=100
var s = 'foo=value1&bar=100'; var o = Ext.decode(s); // o now contains 2 properties, foo and bar. alert(o.bar);
The core provides methods for working with arrays and other collections within Javascript.
Ext.each([1, 2, 3, 4, 5], function(num){
alert(num);
});
var arr1 = Ext.toArray(1); // arr1 = [1]; // arr2 now contains an array of Ext elements. var arr2 = Ext.toArray(Ext.select('div));
JSON stands for Javascript Object Notation. It is used as a data exchange format, where the data looks very similar to javascript object literals. When sending and retrieving data from the server, it is necessary to convert data to and from it's native javascript form. These helper functions assist you to do this. More information about JSON can be found at json.org.
var s = Ext.encode({
foo: 1,
bar: 2
}); //s now contains '{foo=1,bar=2}'
var s = '{foo=1,bar=2}'; var o = Ext.decode(s); o is now an object with 2 properties, foo and bar.
Ext offers a number of browser-detection features allowing developers to work around implementation issues due to differences between the major browsers. Ext provides detection in both javascript and css, to allow for greater functionality in both these areas.
The following browser detection is available for javascript:
if(Ext.isIE){
// Do browser specific code here
}
A similar mechanism is applied in the CSS, various class names are added to the root element and the body depending on the current operating environment. This allows for easier style rules to get around browser quirks. If in strict mode, ext-strict is added to the root. The rest of these are added to the body when appropriate:
/* When in strict mode and using safari, change the font-size. */
.ext-strict .ext-safari .sample-item{
font-size: 20px;
}
Since JavaScript is a loosely typed language, it is often necessary to interrogate variables to retrieve their type. Ext provides a series of methods for help in this regard:
alert(Ext.isEmpty(''));
alert(Ext.isArray([1, 2, 3]));
alert(Ext.isObject({}));
alert(Ext.isFunction(function(){
}));
var s = Ext.id(null, 'prefix'); // No element specified here var s = Ext.id(Ext.get(document.body)); // Assign the id to an element