1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-Object'>/**
2 </span> * @author Jacky Nguyen <jacky@sencha.com>
3 * @docauthor Jacky Nguyen <jacky@sencha.com>
6 * A collection of useful static methods to deal with objects
13 var ExtObject = Ext.Object = {
15 <span id='Ext-Object-method-toQueryObjects'> /**
16 </span> * Convert a `name` - `value` pair to an array of objects with support for nested structures; useful to construct
17 * query strings. For example:
19 var objects = Ext.Object.toQueryObjects('hobbies', ['reading', 'cooking', 'swimming']);
21 // objects then equals:
23 { name: 'hobbies', value: 'reading' },
24 { name: 'hobbies', value: 'cooking' },
25 { name: 'hobbies', value: 'swimming' },
28 var objects = Ext.Object.toQueryObjects('dateOfBirth', {
36 }, true); // Recursive
38 // objects then equals:
40 { name: 'dateOfBirth[day]', value: 3 },
41 { name: 'dateOfBirth[month]', value: 8 },
42 { name: 'dateOfBirth[year]', value: 1987 },
43 { name: 'dateOfBirth[extra][hour]', value: 4 },
44 { name: 'dateOfBirth[extra][minute]', value: 30 },
47 * @param {String} name
48 * @param {Mixed} value
49 * @param {Boolean} recursive
52 toQueryObjects: function(name, value, recursive) {
53 var self = ExtObject.toQueryObjects,
57 if (Ext.isArray(value)) {
58 for (i = 0, ln = value.length; i < ln; i++) {
60 objects = objects.concat(self(name + '[' + i + ']', value[i], true));
70 else if (Ext.isObject(value)) {
72 if (value.hasOwnProperty(i)) {
74 objects = objects.concat(self(name + '[' + i + ']', value[i], true));
95 <span id='Ext-Object-method-toQueryString'> /**
96 </span> * Takes an object and converts it to an encoded query string
100 Ext.Object.toQueryString({foo: 1, bar: 2}); // returns "foo=1&bar=2"
101 Ext.Object.toQueryString({foo: null, bar: 2}); // returns "foo=&bar=2"
102 Ext.Object.toQueryString({'some price': '$300'}); // returns "some%20price=%24300"
103 Ext.Object.toQueryString({date: new Date(2011, 0, 1)}); // returns "date=%222011-01-01T00%3A00%3A00%22"
104 Ext.Object.toQueryString({colors: ['red', 'green', 'blue']}); // returns "colors=red&colors=green&colors=blue"
108 Ext.Object.toQueryString({
115 hobbies: ['coding', 'eating', 'sleeping', ['nested', 'stuff']]
116 }, true); // returns the following string (broken down and url-decoded for ease of reading purpose):
118 // &dateOfBirth[day]=1&dateOfBirth[month]=2&dateOfBirth[year]=1911
119 // &hobbies[0]=coding&hobbies[1]=eating&hobbies[2]=sleeping&hobbies[3][0]=nested&hobbies[3][1]=stuff
122 * @param {Object} object The object to encode
123 * @param {Boolean} recursive (optional) Whether or not to interpret the object in recursive format.
124 * (PHP / Ruby on Rails servers and similar). Defaults to false
125 * @return {String} queryString
128 toQueryString: function(object, recursive) {
129 var paramObjects = [],
131 i, j, ln, paramObject, value;
134 if (object.hasOwnProperty(i)) {
135 paramObjects = paramObjects.concat(ExtObject.toQueryObjects(i, object[i], recursive));
139 for (j = 0, ln = paramObjects.length; j < ln; j++) {
140 paramObject = paramObjects[j];
141 value = paramObject.value;
143 if (Ext.isEmpty(value)) {
146 else if (Ext.isDate(value)) {
147 value = Ext.Date.toString(value);
150 params.push(encodeURIComponent(paramObject.name) + '=' + encodeURIComponent(String(value)));
153 return params.join('&');
156 <span id='Ext-Object-method-fromQueryString'> /**
157 </span> * Converts a query string back into an object.
161 Ext.Object.fromQueryString(foo=1&bar=2); // returns {foo: 1, bar: 2}
162 Ext.Object.fromQueryString(foo=&bar=2); // returns {foo: null, bar: 2}
163 Ext.Object.fromQueryString(some%20price=%24300); // returns {'some price': '$300'}
164 Ext.Object.fromQueryString(colors=red&colors=green&colors=blue); // returns {colors: ['red', 'green', 'blue']}
168 Ext.Object.fromQueryString("username=Jacky&dateOfBirth[day]=1&dateOfBirth[month]=2&dateOfBirth[year]=1911&hobbies[0]=coding&hobbies[1]=eating&hobbies[2]=sleeping&hobbies[3][0]=nested&hobbies[3][1]=stuff", true);
178 hobbies: ['coding', 'eating', 'sleeping', ['nested', 'stuff']]
181 * @param {String} queryString The query string to decode
182 * @param {Boolean} recursive (Optional) Whether or not to recursively decode the string. This format is supported by
183 * PHP / Ruby on Rails servers and similar. Defaults to false
186 fromQueryString: function(queryString, recursive) {
187 var parts = queryString.replace(/^\?/, '').split('&'),
189 temp, components, name, value, i, ln,
190 part, j, subLn, matchedKeys, matchedName,
193 for (i = 0, ln = parts.length; i < ln; i++) {
196 if (part.length > 0) {
197 components = part.split('=');
198 name = decodeURIComponent(components[0]);
199 value = (components[1] !== undefined) ? decodeURIComponent(components[1]) : '';
202 if (object.hasOwnProperty(name)) {
203 if (!Ext.isArray(object[name])) {
204 object[name] = [object[name]];
207 object[name].push(value);
210 object[name] = value;
214 matchedKeys = name.match(/(\[):?([^\]]*)\]/g);
215 matchedName = name.match(/^([^\[]+)/);
217 //<debug error>
220 sourceClass: "Ext.Object",
221 sourceMethod: "fromQueryString",
222 queryString: queryString,
223 recursive: recursive,
224 msg: 'Malformed query string given, failed parsing name from "' + part + '"'
229 name = matchedName[0];
232 if (matchedKeys === null) {
233 object[name] = value;
237 for (j = 0, subLn = matchedKeys.length; j < subLn; j++) {
238 key = matchedKeys[j];
239 key = (key.length === 2) ? '' : key.substring(1, key.length - 1);
247 for (j = 0, subLn = keys.length; j < subLn; j++) {
250 if (j === subLn - 1) {
251 if (Ext.isArray(temp) && key === '') {
259 if (temp[key] === undefined || typeof temp[key] === 'string') {
262 temp[key] = (Ext.isNumeric(nextKey) || nextKey === '') ? [] : {};
275 <span id='Ext-Object-method-each'> /**
276 </span> * Iterate through an object and invoke the given callback function for each iteration. The iteration can be stop
277 * by returning `false` in the callback function. For example:
282 loves: ['food', 'sleeping', 'wife']
285 Ext.Object.each(person, function(key, value, myself) {
286 console.log(key + ":" + value);
288 if (key === 'hairColor') {
289 return false; // stop the iteration
293 * @param {Object} object The object to iterate
294 * @param {Function} fn The callback function. Passed arguments for each iteration are:
298 - {Object} `object` The object itself
300 * @param {Object} scope (Optional) The execution scope (`this`) of the callback function
303 each: function(object, fn, scope) {
304 for (var property in object) {
305 if (object.hasOwnProperty(property)) {
306 if (fn.call(scope || object, property, object[property], object) === false) {
313 <span id='Ext-Object-method-merge'> /**
314 </span> * Merges any number of objects recursively without referencing them or their children.
317 companyName: 'Ext JS',
318 products: ['Ext JS', 'Ext GWT', 'Ext Designer'],
322 location: 'Palo Alto',
328 companyName: 'Sencha Inc.',
329 products: ['Ext JS', 'Ext GWT', 'Ext Designer', 'Sencha Touch', 'Sencha Animator'],
332 location: 'Redwood City'
336 var sencha = Ext.Object.merge(extjs, newStuff);
338 // extjs and sencha then equals to
340 companyName: 'Sencha Inc.',
341 products: ['Ext JS', 'Ext GWT', 'Ext Designer', 'Sencha Touch', 'Sencha Animator'],
345 location: 'Redwood City'
350 * @param {Object} object,...
351 * @return {Object} merged The object that is created as a result of merging all the objects passed in.
354 merge: function(source, key, value) {
355 if (typeof key === 'string') {
356 if (value && value.constructor === Object) {
357 if (source[key] && source[key].constructor === Object) {
358 ExtObject.merge(source[key], value);
361 source[key] = Ext.clone(value);
372 ln = arguments.length,
375 for (; i < ln; i++) {
376 object = arguments[i];
378 for (property in object) {
379 if (object.hasOwnProperty(property)) {
380 ExtObject.merge(source, property, object[property]);
388 <span id='Ext-Object-method-getKey'> /**
389 </span> * Returns the first matching key corresponding to the given value.
390 * If no matching value is found, null is returned.
397 alert(Ext.Object.getKey(sencha, 'loves')); // alerts 'food'
399 * @param {Object} object
400 * @param {Object} value The value to find
403 getKey: function(object, value) {
404 for (var property in object) {
405 if (object.hasOwnProperty(property) && object[property] === value) {
413 <span id='Ext-Object-method-getValues'> /**
414 </span> * Gets all values of the given object as an array.
416 var values = Ext.Object.getValues({
419 }); // ['Jacky', 'food']
421 * @param {Object} object
422 * @return {Array} An array of values from the object
425 getValues: function(object) {
429 for (property in object) {
430 if (object.hasOwnProperty(property)) {
431 values.push(object[property]);
438 <span id='Ext-Object-property-getKeys'> /**
439 </span> * Gets all keys of the given object as an array.
441 var values = Ext.Object.getKeys({
444 }); // ['name', 'loves']
446 * @param {Object} object
447 * @return {Array} An array of keys from the object
449 getKeys: ('keys' in Object.prototype) ? Object.keys : function(object) {
453 for (property in object) {
454 if (object.hasOwnProperty(property)) {
462 <span id='Ext-Object-method-getSize'> /**
463 </span> * Gets the total number of this object's own properties
465 var size = Ext.Object.getSize({
470 * @param {Object} object
471 * @return {Number} size
474 getSize: function(object) {
478 for (property in object) {
479 if (object.hasOwnProperty(property)) {
489 <span id='Ext-method-merge'>/**
490 </span> * A convenient alias method for {@link Ext.Object#merge}
495 Ext.merge = Ext.Object.merge;
497 <span id='Ext-method-urlEncode'>/**
498 </span> * A convenient alias method for {@link Ext.Object#toQueryString}
502 * @deprecated 4.0.0 Use {@link Ext.Object#toQueryString Ext.Object.toQueryString} instead
504 Ext.urlEncode = function() {
505 var args = Ext.Array.from(arguments),
508 // Support for the old `pre` argument
509 if ((typeof args[1] === 'string')) {
510 prefix = args[1] + '&';
514 return prefix + Ext.Object.toQueryString.apply(Ext.Object, args);
517 <span id='Ext-method-urlDecode'>/**
518 </span> * A convenient alias method for {@link Ext.Object#fromQueryString}
522 * @deprecated 4.0.0 Use {@link Ext.Object#fromQueryString Ext.Object.fromQueryString} instead
524 Ext.urlDecode = function() {
525 return Ext.Object.fromQueryString.apply(Ext.Object, arguments);
529 </pre></pre></body></html>