4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-Object'>/**
19 </span> * @author Jacky Nguyen <jacky@sencha.com>
20 * @docauthor Jacky Nguyen <jacky@sencha.com>
23 * A collection of useful static methods to deal with objects
30 var ExtObject = Ext.Object = {
32 <span id='Ext-Object-method-toQueryObjects'> /**
33 </span> * Convert a `name` - `value` pair to an array of objects with support for nested structures; useful to construct
34 * query strings. For example:
36 var objects = Ext.Object.toQueryObjects('hobbies', ['reading', 'cooking', 'swimming']);
38 // objects then equals:
40 { name: 'hobbies', value: 'reading' },
41 { name: 'hobbies', value: 'cooking' },
42 { name: 'hobbies', value: 'swimming' },
45 var objects = Ext.Object.toQueryObjects('dateOfBirth', {
53 }, true); // Recursive
55 // objects then equals:
57 { name: 'dateOfBirth[day]', value: 3 },
58 { name: 'dateOfBirth[month]', value: 8 },
59 { name: 'dateOfBirth[year]', value: 1987 },
60 { name: 'dateOfBirth[extra][hour]', value: 4 },
61 { name: 'dateOfBirth[extra][minute]', value: 30 },
64 * @param {String} name
65 * @param {Mixed} value
66 * @param {Boolean} recursive
69 toQueryObjects: function(name, value, recursive) {
70 var self = ExtObject.toQueryObjects,
74 if (Ext.isArray(value)) {
75 for (i = 0, ln = value.length; i < ln; i++) {
77 objects = objects.concat(self(name + '[' + i + ']', value[i], true));
87 else if (Ext.isObject(value)) {
89 if (value.hasOwnProperty(i)) {
91 objects = objects.concat(self(name + '[' + i + ']', value[i], true));
112 <span id='Ext-Object-method-toQueryString'> /**
113 </span> * Takes an object and converts it to an encoded query string
117 Ext.Object.toQueryString({foo: 1, bar: 2}); // returns "foo=1&bar=2"
118 Ext.Object.toQueryString({foo: null, bar: 2}); // returns "foo=&bar=2"
119 Ext.Object.toQueryString({'some price': '$300'}); // returns "some%20price=%24300"
120 Ext.Object.toQueryString({date: new Date(2011, 0, 1)}); // returns "date=%222011-01-01T00%3A00%3A00%22"
121 Ext.Object.toQueryString({colors: ['red', 'green', 'blue']}); // returns "colors=red&colors=green&colors=blue"
125 Ext.Object.toQueryString({
132 hobbies: ['coding', 'eating', 'sleeping', ['nested', 'stuff']]
133 }, true); // returns the following string (broken down and url-decoded for ease of reading purpose):
135 // &dateOfBirth[day]=1&dateOfBirth[month]=2&dateOfBirth[year]=1911
136 // &hobbies[0]=coding&hobbies[1]=eating&hobbies[2]=sleeping&hobbies[3][0]=nested&hobbies[3][1]=stuff
139 * @param {Object} object The object to encode
140 * @param {Boolean} recursive (optional) Whether or not to interpret the object in recursive format.
141 * (PHP / Ruby on Rails servers and similar). Defaults to false
142 * @return {String} queryString
145 toQueryString: function(object, recursive) {
146 var paramObjects = [],
148 i, j, ln, paramObject, value;
151 if (object.hasOwnProperty(i)) {
152 paramObjects = paramObjects.concat(ExtObject.toQueryObjects(i, object[i], recursive));
156 for (j = 0, ln = paramObjects.length; j < ln; j++) {
157 paramObject = paramObjects[j];
158 value = paramObject.value;
160 if (Ext.isEmpty(value)) {
163 else if (Ext.isDate(value)) {
164 value = Ext.Date.toString(value);
167 params.push(encodeURIComponent(paramObject.name) + '=' + encodeURIComponent(String(value)));
170 return params.join('&');
173 <span id='Ext-Object-method-fromQueryString'> /**
174 </span> * Converts a query string back into an object.
178 Ext.Object.fromQueryString(foo=1&bar=2); // returns {foo: 1, bar: 2}
179 Ext.Object.fromQueryString(foo=&bar=2); // returns {foo: null, bar: 2}
180 Ext.Object.fromQueryString(some%20price=%24300); // returns {'some price': '$300'}
181 Ext.Object.fromQueryString(colors=red&colors=green&colors=blue); // returns {colors: ['red', 'green', 'blue']}
185 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);
195 hobbies: ['coding', 'eating', 'sleeping', ['nested', 'stuff']]
198 * @param {String} queryString The query string to decode
199 * @param {Boolean} recursive (Optional) Whether or not to recursively decode the string. This format is supported by
200 * PHP / Ruby on Rails servers and similar. Defaults to false
203 fromQueryString: function(queryString, recursive) {
204 var parts = queryString.replace(/^\?/, '').split('&'),
206 temp, components, name, value, i, ln,
207 part, j, subLn, matchedKeys, matchedName,
210 for (i = 0, ln = parts.length; i < ln; i++) {
213 if (part.length > 0) {
214 components = part.split('=');
215 name = decodeURIComponent(components[0]);
216 value = (components[1] !== undefined) ? decodeURIComponent(components[1]) : '';
219 if (object.hasOwnProperty(name)) {
220 if (!Ext.isArray(object[name])) {
221 object[name] = [object[name]];
224 object[name].push(value);
227 object[name] = value;
231 matchedKeys = name.match(/(\[):?([^\]]*)\]/g);
232 matchedName = name.match(/^([^\[]+)/);
234 //<debug error>
237 sourceClass: "Ext.Object",
238 sourceMethod: "fromQueryString",
239 queryString: queryString,
240 recursive: recursive,
241 msg: 'Malformed query string given, failed parsing name from "' + part + '"'
246 name = matchedName[0];
249 if (matchedKeys === null) {
250 object[name] = value;
254 for (j = 0, subLn = matchedKeys.length; j < subLn; j++) {
255 key = matchedKeys[j];
256 key = (key.length === 2) ? '' : key.substring(1, key.length - 1);
264 for (j = 0, subLn = keys.length; j < subLn; j++) {
267 if (j === subLn - 1) {
268 if (Ext.isArray(temp) && key === '') {
276 if (temp[key] === undefined || typeof temp[key] === 'string') {
279 temp[key] = (Ext.isNumeric(nextKey) || nextKey === '') ? [] : {};
292 <span id='Ext-Object-method-each'> /**
293 </span> * Iterate through an object and invoke the given callback function for each iteration. The iteration can be stop
294 * by returning `false` in the callback function. For example:
299 loves: ['food', 'sleeping', 'wife']
302 Ext.Object.each(person, function(key, value, myself) {
303 console.log(key + ":" + value);
305 if (key === 'hairColor') {
306 return false; // stop the iteration
310 * @param {Object} object The object to iterate
311 * @param {Function} fn The callback function. Passed arguments for each iteration are:
315 - {Object} `object` The object itself
317 * @param {Object} scope (Optional) The execution scope (`this`) of the callback function
320 each: function(object, fn, scope) {
321 for (var property in object) {
322 if (object.hasOwnProperty(property)) {
323 if (fn.call(scope || object, property, object[property], object) === false) {
330 <span id='Ext-Object-method-merge'> /**
331 </span> * Merges any number of objects recursively without referencing them or their children.
334 companyName: 'Ext JS',
335 products: ['Ext JS', 'Ext GWT', 'Ext Designer'],
339 location: 'Palo Alto',
345 companyName: 'Sencha Inc.',
346 products: ['Ext JS', 'Ext GWT', 'Ext Designer', 'Sencha Touch', 'Sencha Animator'],
349 location: 'Redwood City'
353 var sencha = Ext.Object.merge(extjs, newStuff);
355 // extjs and sencha then equals to
357 companyName: 'Sencha Inc.',
358 products: ['Ext JS', 'Ext GWT', 'Ext Designer', 'Sencha Touch', 'Sencha Animator'],
362 location: 'Redwood City'
367 * @param {Object} object,...
368 * @return {Object} merged The object that is created as a result of merging all the objects passed in.
371 merge: function(source, key, value) {
372 if (typeof key === 'string') {
373 if (value && value.constructor === Object) {
374 if (source[key] && source[key].constructor === Object) {
375 ExtObject.merge(source[key], value);
378 source[key] = Ext.clone(value);
389 ln = arguments.length,
392 for (; i < ln; i++) {
393 object = arguments[i];
395 for (property in object) {
396 if (object.hasOwnProperty(property)) {
397 ExtObject.merge(source, property, object[property]);
405 <span id='Ext-Object-method-getKey'> /**
406 </span> * Returns the first matching key corresponding to the given value.
407 * If no matching value is found, null is returned.
414 alert(Ext.Object.getKey(sencha, 'loves')); // alerts 'food'
416 * @param {Object} object
417 * @param {Object} value The value to find
420 getKey: function(object, value) {
421 for (var property in object) {
422 if (object.hasOwnProperty(property) && object[property] === value) {
430 <span id='Ext-Object-method-getValues'> /**
431 </span> * Gets all values of the given object as an array.
433 var values = Ext.Object.getValues({
436 }); // ['Jacky', 'food']
438 * @param {Object} object
439 * @return {Array} An array of values from the object
442 getValues: function(object) {
446 for (property in object) {
447 if (object.hasOwnProperty(property)) {
448 values.push(object[property]);
455 <span id='Ext-Object-method-getKeys'> /**
456 </span> * Gets all keys of the given object as an array.
458 var values = Ext.Object.getKeys({
461 }); // ['name', 'loves']
463 * @param {Object} object
464 * @return {Array} An array of keys from the object
467 getKeys: ('keys' in Object.prototype) ? Object.keys : function(object) {
471 for (property in object) {
472 if (object.hasOwnProperty(property)) {
480 <span id='Ext-Object-method-getSize'> /**
481 </span> * Gets the total number of this object's own properties
483 var size = Ext.Object.getSize({
488 * @param {Object} object
489 * @return {Number} size
492 getSize: function(object) {
496 for (property in object) {
497 if (object.hasOwnProperty(property)) {
507 <span id='Ext-method-merge'>/**
508 </span> * A convenient alias method for {@link Ext.Object#merge}
513 Ext.merge = Ext.Object.merge;
515 <span id='Ext-method-urlEncode'>/**
516 </span> * A convenient alias method for {@link Ext.Object#toQueryString}
520 * @deprecated 4.0.0 Use {@link Ext.Object#toQueryString Ext.Object.toQueryString} instead
522 Ext.urlEncode = function() {
523 var args = Ext.Array.from(arguments),
526 // Support for the old `pre` argument
527 if ((typeof args[1] === 'string')) {
528 prefix = args[1] + '&';
532 return prefix + Ext.Object.toQueryString.apply(Ext.Object, args);
535 <span id='Ext-method-urlDecode'>/**
536 </span> * A convenient alias method for {@link Ext.Object#fromQueryString}
540 * @deprecated 4.0.0 Use {@link Ext.Object#fromQueryString Ext.Object.fromQueryString} instead
542 Ext.urlDecode = function() {
543 return Ext.Object.fromQueryString.apply(Ext.Object, arguments);